简体   繁体   English

paper.js和javascript'this'混淆

[英]paper.js and javascript 'this' confusion

Hi I am creating a Cabinet object in javascript and I am trying to access the objects property inside paper.js' onFrame function... But when I use 'this' inside the onFrame function I can not access the object property: 嗨,我正在用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
    }
  }
}

How can I achieve this? 我该如何实现?

Simply save the value of this in a variable: 只需保存的值, this在一个变量:

Cabinet.prototype = {
  draw:function(){
    var cab = this;
    this.spine.onFrame = function(event) {
      cab.spine // ...
    }
  }
}

You could bind the function so that this becomes a reference to the Cabinet object. 你可以绑定功能,使this成为内阁对象的引用。

Cabinet.prototype = {
  draw: function() {
    view.onFrame = function(event) {
      this.spine;
    }.bind(this);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM