Skip to content

Commit 7a1b2be

Browse files
committed
exec: what-is-the-purpose-of-the-new-keyword
1 parent 38f2a07 commit 7a1b2be

File tree

1 file changed

+5
-5
lines changed
  • questions/what-is-the-purpose-of-the-new-keyword

1 file changed

+5
-5
lines changed

questions/what-is-the-purpose-of-the-new-keyword/en-US.mdx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ title: What is the purpose of the `new` keyword?
66

77
The `new` keyword in JavaScript is used to create an instance of a user-defined object type or one of the built-in object types that has a constructor function. When you use `new`, it does four things: it creates a new object, sets the prototype, binds `this` to the new object, and returns the new object.
88

9-
```js
9+
```js live
1010
function Person(name) {
1111
this.name = name;
1212
}
@@ -23,7 +23,7 @@ console.log(person1.name); // Alice
2323

2424
The `new` keyword is used to create a new instance of an object. When you call a constructor function with `new`, it creates a new object.
2525

26-
```js
26+
```js live
2727
function Car(model) {
2828
this.model = model;
2929
}
@@ -36,7 +36,7 @@ console.log(myCar.model); // Toyota
3636

3737
The new object’s internal `[[Prototype]]` property is set to the constructor function’s `prototype` property. This allows the new object to inherit properties and methods from the constructor’s prototype.
3838

39-
```js
39+
```js live
4040
function Animal(type) {
4141
this.type = type;
4242
}
@@ -53,7 +53,7 @@ dog.speak(); // Dog makes a sound
5353

5454
Inside the constructor function, `this` refers to the new object that is being created. This allows you to add properties and methods to the new object.
5555

56-
```js
56+
```js live
5757
function Book(title) {
5858
this.title = title;
5959
}
@@ -66,7 +66,7 @@ console.log(myBook.title); // JavaScript Essentials
6666

6767
The `new` keyword implicitly returns the new object created by the constructor function. If the constructor function explicitly returns an object, that object will be returned instead.
6868

69-
```js
69+
```js live
7070
function Gadget(name) {
7171
this.name = name;
7272
return { type: 'Electronic' };

0 commit comments

Comments
 (0)