You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: questions/what-are-the-differences-between-es6-class-and-es5-function-constructors/en-US.mdx
+55-40Lines changed: 55 additions & 40 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,28 +20,47 @@ class Person {
20
20
}
21
21
```
22
22
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.
24
24
25
-
```js
26
-
// ES5 function constructor
27
-
functionStudent(name, studentId) {
25
+
```js live
26
+
// ES5 inheritance
27
+
// Superclass
28
+
functionPerson1(name) {
29
+
this.name= name;
30
+
}
31
+
32
+
// Subclass
33
+
functionStudent1(name, studentId) {
28
34
// Call constructor of superclass to initialize superclass-derived members.
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
67
86
68
87
In ES5, you define a class-like structure using a function constructor and prototypes. Here's an example:
69
88
70
-
```js
89
+
```js live
71
90
// ES5 function constructor
72
91
functionPerson(name, age) {
73
92
this.name= name;
@@ -89,7 +108,7 @@ person1.greet(); // Hello, my name is John and I am 30 years old.
89
108
90
109
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:
91
110
92
-
```js
111
+
```js live
93
112
// ES2015 Class
94
113
classPerson {
95
114
constructor(name, age) {
@@ -121,72 +140,68 @@ person1.greet(); // Hello, my name is John and I am 30 years old.
121
140
-**ES5**: Static methods are added directly to the constructor function.
122
141
-**ES2015**: Static methods are defined within the class using the `static` keyword.
123
142
124
-
```js
143
+
```js live
125
144
// ES5
126
-
functionPerson(name, age) {
145
+
functionPerson1(name, age) {
127
146
this.name= name;
128
147
this.age= age;
129
148
}
130
149
131
-
Person.sayHi=function () {
132
-
console.log('Hi!');
150
+
Person1.sayHi=function () {
151
+
console.log('Hi from ES5!');
133
152
};
134
153
135
-
Person.sayHi(); // Hi!
154
+
Person1.sayHi(); // Hi from ES5!
136
155
137
156
// ES2015
138
-
classPerson {
157
+
classPerson2 {
139
158
staticsayHi() {
140
-
console.log('Hi!');
159
+
console.log('Hi from ES2015!');
141
160
}
142
161
}
143
-
Person.sayHi(); // Hi!
162
+
Person2.sayHi(); // Hi from ES2015!
144
163
```
145
164
146
165
3.**Inheritance**
147
166
148
167
-**ES5**: Inheritance is achieved using `Object.create()` and manually setting the prototype chain.
149
168
-**ES2015**: Inheritance is much simpler and more intuitive with the extends keyword.
150
169
151
-
```js
170
+
```js live
152
171
// ES5 Inheritance
153
172
154
173
// ES5 function constructor
155
-
functionPerson(name, age) {
174
+
functionPerson1(name, age) {
156
175
this.name= name;
157
176
this.age= age;
158
177
}
159
178
160
-
Person.prototype.greet=function () {
179
+
Person1.prototype.greet=function () {
161
180
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.`,
0 commit comments