嗨,我正在 javascript 中创建一个 Cabinet 对象,并且尝试访问 paper.js 的 onFrame 函数内的对象属性...但是当我在 onFrame 函数内使用“this”时,我无法访问对象属性:
function Cabinet(){
this.spine = new Path({x:0, y:0});
this.draw();
return this;
}
Cabinet.prototype = {
draw:function(){
view.onFrame = function(event) {
this.spine // trying to access Cabinet object but this is not referring to Cabinet object
}
}
}
我怎样才能实现这个目标?
最佳答案
您可以绑定(bind)该函数,以便 this
成为对 Cabinet 对象的引用。
Cabinet.prototype = {
draw: function() {
view.onFrame = function(event) {
this.spine;
}.bind(this);
}
}
关于javascript - paper.js 和 javascript 'this' 混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24327556/