【发布时间】:2017-03-19 17:12:17
【问题描述】:
我们如何将 es6 类方法填充到 ES5 中?
我正在看一本书,上面写着:
class Ninja {
constructor(name) {
this.name = name;
}
swingSword() {
return true;
}
}
和
一样function Ninja(name) {
this.name = name;
}
Ninja.prototype.swingSword = function() {
return true;
};
我只是问为什么我们在原型上而不是在构造函数中添加 swingSword?
因为函数应该在对象上,而不是在原型链上。
我是对还是错?
【问题讨论】:
-
你错了,这本书是对的。
标签: javascript ecmascript-6 es6-class