ES5中生成实例对象的方法是通过构造函数:
function Person(name, age){ this.name = name this.age = age } Person.prototype.sayName = function () { console.log(this.name) }
ES6中添加新的语法生成对象实例:
class Person { constructor (name, age) { this.name = name this.age = age } sayName () { console.log(this.name) } }
时间: 2024-10-30 21:50:07