ctor
js
// before ES2015
function Person(name, age) {
this.name = name;
this.age = age;
this.toString = () => {
return `I am ${name}, I am ${age} years old!`;
}
}
const person = new Person('john', 18);
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
js
// after ES2015
class Person {
name;
age;
constructor (name, age) {
this.name = name;
this.age = age;
}
toString() {
return `I am ${this.name}, I am ${this.age} years old!`;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12