一、箭頭函數
1、沒有this
二、class
class Person {
constructor(name, age) { // 構造函數
this.name = name;
this.age = age;
}
getName() { // 這種寫法表示將方法添加到原型中
return this.name
}
static a = 20; // 等同於 Person.a = 20
c = 20; // 表示在構造函數中添加屬性 在構造函數中等同於 this.c = 20
// 箭頭函數的寫法表示在構造函數中添加方法,在構造函數中等同於this.getAge = function() {}
getAge = () => this.age
}
// ES5
// 構造函數
function Person(name, age) {
this.name = name;
this.age = age;
}
// 原型方法
Person.prototype.getName = function() {
return this.name
}
// ES6
class Person {
constructor(name, age) { // 構造函數
this.name = name;
this.age = age;
}
getName() { // 原型方法
return this.name
}
}