Skip to content

Commit 208848a

Browse files
committed
exec: what-are-the-differences-between-es6-class-and-es5-function-constructors
1 parent 4b864fa commit 208848a

File tree

1 file changed

+55
-40
lines changed
  • questions/what-are-the-differences-between-es6-class-and-es5-function-constructors

1 file changed

+55
-40
lines changed

questions/what-are-the-differences-between-es6-class-and-es5-function-constructors/en-US.mdx

Lines changed: 55 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,47 @@ class Person {
2020
}
2121
```
2222

23-
For simple constructors, they look pretty similar. The main difference in the constructor comes when using inheritance. If we want to create a `Student` class that subclasses `Person` and add a `studentId` field, this is what we have to do in addition to the above.
23+
For simple constructors, they look pretty similar. The main difference in the constructor comes when using inheritance. If we want to create a `Student` class that subclasses `Person` and add a `studentId` field, this is what we have to do.
2424

25-
```js
26-
// ES5 function constructor
27-
function Student(name, studentId) {
25+
```js live
26+
// ES5 inheritance
27+
// Superclass
28+
function Person1(name) {
29+
this.name = name;
30+
}
31+
32+
// Subclass
33+
function Student1(name, studentId) {
2834
// Call constructor of superclass to initialize superclass-derived members.
29-
Person.call(this, name);
35+
Person1.call(this, name);
3036

3137
// Initialize subclass's own members.
3238
this.studentId = studentId;
3339
}
40+
Student1.prototype = Object.create(Person1.prototype);
41+
Student1.prototype.constructor = Student1;
3442

35-
Student.prototype = Object.create(Person.prototype);
36-
Student.prototype.constructor = Student;
43+
const student1 = new Student1('John', 1234);
44+
console.log(student1.name, student1.studentId); // "John" 1234
3745

38-
// ES2015 Class
39-
class Student extends Person {
46+
// ES2015 inheritance
47+
// Superclass
48+
class Person2 {
49+
constructor(name) {
50+
this.name = name;
51+
}
52+
}
53+
54+
// Subclass
55+
class Student2 extends Person2 {
4056
constructor(name, studentId) {
4157
super(name);
4258
this.studentId = studentId;
4359
}
4460
}
61+
62+
const student2 = new Student2('Alice', 5678);
63+
console.log(student2.name, student2.studentId); // "Alice" 5678
4564
```
4665

4766
It's much more verbose to use inheritance in ES5 and the ES2015 version is easier to understand and remember.
@@ -67,7 +86,7 @@ ES5 function constructors and ES2015 classes are two different ways of defining
6786

6887
In ES5, you define a class-like structure using a function constructor and prototypes. Here's an example:
6988

70-
```js
89+
```js live
7190
// ES5 function constructor
7291
function Person(name, age) {
7392
this.name = name;
@@ -89,7 +108,7 @@ person1.greet(); // Hello, my name is John and I am 30 years old.
89108

90109
ES2015 introduced the `class` syntax, which simplifies the definition of classes and supports more features such as static methods and subclassing. Here's the same example using ES2015:
91110

92-
```js
111+
```js live
93112
// ES2015 Class
94113
class Person {
95114
constructor(name, age) {
@@ -121,72 +140,68 @@ person1.greet(); // Hello, my name is John and I am 30 years old.
121140
- **ES5**: Static methods are added directly to the constructor function.
122141
- **ES2015**: Static methods are defined within the class using the `static` keyword.
123142

124-
```js
143+
```js live
125144
// ES5
126-
function Person(name, age) {
145+
function Person1(name, age) {
127146
this.name = name;
128147
this.age = age;
129148
}
130149

131-
Person.sayHi = function () {
132-
console.log('Hi!');
150+
Person1.sayHi = function () {
151+
console.log('Hi from ES5!');
133152
};
134153

135-
Person.sayHi(); // Hi!
154+
Person1.sayHi(); // Hi from ES5!
136155

137156
// ES2015
138-
class Person {
157+
class Person2 {
139158
static sayHi() {
140-
console.log('Hi!');
159+
console.log('Hi from ES2015!');
141160
}
142161
}
143-
Person.sayHi(); // Hi!
162+
Person2.sayHi(); // Hi from ES2015!
144163
```
145164

146165
3. **Inheritance**
147166

148167
- **ES5**: Inheritance is achieved using `Object.create()` and manually setting the prototype chain.
149168
- **ES2015**: Inheritance is much simpler and more intuitive with the extends keyword.
150169

151-
```js
170+
```js live
152171
// ES5 Inheritance
153172

154173
// ES5 function constructor
155-
function Person(name, age) {
174+
function Person1(name, age) {
156175
this.name = name;
157176
this.age = age;
158177
}
159178

160-
Person.prototype.greet = function () {
179+
Person1.prototype.greet = function () {
161180
console.log(
162-
'Hello, my name is ' +
163-
this.name +
164-
' and I am ' +
165-
this.age +
166-
' years old.',
181+
`Hello, my name is ${this.name} and I am ${this.age} years old.`,
167182
);
168183
};
169184

170-
function Student(name, age, grade) {
171-
Person.call(this, name, age);
185+
function Student1(name, age, grade) {
186+
Person1.call(this, name, age);
172187
this.grade = grade;
173188
}
174189

175-
Student.prototype = Object.create(Person.prototype);
176-
Student.prototype.constructor = Student;
190+
Student1.prototype = Object.create(Person1.prototype);
191+
Student1.prototype.constructor = Student1;
177192

178-
Student.prototype.study = function () {
193+
Student1.prototype.study = function () {
179194
console.log(this.name + ' is studying.');
180195
};
181196

182-
var student1 = new Student('Alice', 20, 'A');
183-
student1.greet(); // Hello, my name is Alice and I am 20 years old.
184-
student1.study(); // Alice is studying.
197+
var student1 = new Student1('John', 22, 'B+');
198+
student1.greet(); // Hello, my name is John and I am 22 years old.
199+
student1.study(); // John is studying.
185200

186201
// ES2015 Inheritance
187202

188203
// ES2015 Class
189-
class Person {
204+
class Person2 {
190205
constructor(name, age) {
191206
this.name = name;
192207
this.age = age;
@@ -199,7 +214,7 @@ person1.greet(); // Hello, my name is John and I am 30 years old.
199214
}
200215
}
201216

202-
class Student extends Person {
217+
class Student2 extends Person2 {
203218
constructor(name, age, grade) {
204219
super(name, age);
205220
this.grade = grade;
@@ -210,9 +225,9 @@ person1.greet(); // Hello, my name is John and I am 30 years old.
210225
}
211226
}
212227

213-
const student1 = new Student('Alice', 20, 'A');
214-
student1.greet(); // Hello, my name is Alice and I am 20 years old.
215-
student1.study(); // Alice is studying.
228+
const student2 = new Student2('Alice', 20, 'A');
229+
student2.greet(); // Hello, my name is Alice and I am 20 years old.
230+
student2.study(); // Alice is studying.
216231
```
217232

218233
4. `super` calls:

0 commit comments

Comments
 (0)