Skip to content

Commit 4b864fa

Browse files
committed
exec: how-do-you-create-a-constructor-function
1 parent 7a1b2be commit 4b864fa

File tree

1 file changed

+23
-6
lines changed
  • questions/how-do-you-create-a-constructor-function

1 file changed

+23
-6
lines changed

questions/how-do-you-create-a-constructor-function/en-US.mdx

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ title: How do you create a constructor function?
66

77
To create a constructor function in JavaScript, define a regular function with a capitalized name to indicate it's a constructor. Use the `this` keyword to set properties and methods. When creating an instance, use the `new` keyword.
88

9-
```js
9+
```js live
1010
function Person(name, age) {
1111
this.name = name;
1212
this.age = age;
1313
}
1414

1515
const john = new Person('John', 30);
16+
console.log(john.age); // 30
1617
```
1718

1819
---
@@ -23,7 +24,7 @@ const john = new Person('John', 30);
2324

2425
A constructor function in JavaScript is a regular function that is used to create objects. By convention, the name of the constructor function starts with a capital letter to distinguish it from regular functions.
2526

26-
```js
27+
```js live
2728
function Person(name, age) {
2829
this.name = name;
2930
this.age = age;
@@ -34,7 +35,7 @@ function Person(name, age) {
3435

3536
Within the constructor function, the `this` keyword is used to refer to the object that will be created. Properties and methods can be assigned to `this`.
3637

37-
```js
38+
```js live
3839
function Person(name, age) {
3940
this.name = name;
4041
this.age = age;
@@ -50,7 +51,17 @@ function Person(name, age) {
5051

5152
To create an instance of the object, use the `new` keyword followed by the constructor function.
5253

53-
```js
54+
```js live
55+
function Person(name, age) {
56+
this.name = name;
57+
this.age = age;
58+
this.greet = function () {
59+
console.log(
60+
`Hello, my name is ${this.name} and I am ${this.age} years old.`,
61+
);
62+
};
63+
}
64+
5465
const john = new Person('John', 30);
5566
john.greet(); // Output: Hello, my name is John and I am 30 years old.
5667
```
@@ -59,7 +70,7 @@ john.greet(); // Output: Hello, my name is John and I am 30 years old.
5970

6071
To save memory, it's a good practice to add methods to the constructor's prototype instead of defining them inside the constructor function.
6172

62-
```js
73+
```js live
6374
function Person(name, age) {
6475
this.name = name;
6576
this.age = age;
@@ -77,7 +88,13 @@ jane.greet(); // Output: Hello, my name is Jane and I am 25 years old.
7788

7889
You can check if an object is an instance of a constructor function using the `instanceof` operator.
7990

80-
```js
91+
```js live
92+
function Person(name, age) {
93+
this.name = name;
94+
this.age = age;
95+
}
96+
const jane = new Person('Jane', 25);
97+
8198
console.log(jane instanceof Person); // Output: true
8299
```
83100

0 commit comments

Comments
 (0)