Sian 发表于 2018-1-13 15:52:53

Javascript中ES5与ES6创建类的两种不同方式

1、ES5使用构造函数模式与原型模式相结合
// 构造函数模式
function Person(name, age){
this.name = name;
this.age = age;
}
// 原型模式
Person.prototype = {
constructor:Person,
print(){
    console.log('...');
}
}

2、ES6中使用class关键字,类似高级语言
// class方法
class Person{
constructor(name, age){
    this.name = name;
    this.age = age;
}
print(){
    console.log("...");
}
}

页: [1]
查看完整版本: Javascript中ES5与ES6创建类的两种不同方式