javascript - paper.js 和 javascript 'this' 混淆

标签 javascript paperjs

嗨,我正在 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/

相关文章:

javascript - window.opener.parent 在 Microsoft Edge 上不起作用

javascript - ES6 : Why Symbol can not have another symbol as description?

javascript - 如何更改 map 上的省份名称?

javascript - 纸.js : hitTest for obstructed items with mouse event?

javascript - 如何在javascript中生成随机灰色

javascript - 使用 paperjs 翻译 Canvas 中的坐标系

php - 如何修复 PHP 表单验证器错误?

javascript - Paper JS 的编程点击事件

javascript - PaperJS SymbolItems 在命中结果搜索后丢失属性

javascript - 在 CSS IE6 问题上需要帮助