我有一个简单的原型继承构造器,我使用箭头函数。
app.Node = function (name, type) {
this.name = name;
this.type = type;
this.children = [];
}
app.Node.prototype = {
addChild: (child)=>{
child._parent = this;
this.children.push(child);
},
removeChild: (child)=>{
this.children.forEach((item, i) => {
if (item === child) {
this.removeChildAtIndex(i);
}
});
},
}但是,由于this和箭头函数的性质,在上面的原型方法中,this的值是undefined。那么我能以这种方式使用箭头函数吗?除了使用普通的function函数之外,我不知道需要修改什么。
发布于 2016-12-02 21:48:48
所以我能以这种方式使用箭头函数吗?
不是的。箭头函数在声明时捕获this的值。
我不知道我需要修改什么,除了使用普通的函数。
就这么做吧。使用合适的工具来完成工作。
https://stackoverflow.com/questions/40933690
复制相似问题