您好我在javascript中创建一个Cabinet对象,我正在尝试访问paper.js'onFrame函数中的objects属性...但是当我在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
}
}
}
我怎样才能做到这一点?
答案 0 :(得分:1)
只需将this
的值保存在变量中:
Cabinet.prototype = {
draw:function(){
var cab = this;
this.spine.onFrame = function(event) {
cab.spine // ...
}
}
}
答案 1 :(得分:1)
您可以绑定该函数,以便this
成为Cabinet对象的引用。
Cabinet.prototype = {
draw: function() {
view.onFrame = function(event) {
this.spine;
}.bind(this);
}
}