我通过以下方式声明了一个类:
function maskEditor() {
this.init();
};
maskEditor.prototype = {
foo: null,
container: new createjs.Container(),
init:function () {
this.foo = "bar";
this.container.on("mousedown", this.onMouseDown); // This is just an easeljs event dispatcher
},
onMouseDown: function (event) {
alert(this.foo); // WRONG. 'this' is out of the scope :(
}
};
长话短说:我使用easeljs库,并且我已经声明了一个事件调度程序来捕获鼠标点击。我需要从该方法内部访问 maskEditor 对象。我怎样才能做到这一点?
最佳答案
您需要将上下文绑定(bind)到事件处理程序:
this.container.on("mousedown", this.onMouseDown.bind(this));
关于javascript - 如何从 Javascript 的内部函数访问父对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31189981/