0
我正在爲一個小遊戲構建一個事件管理器,我正在創建一個小遊戲並且偶然發現了一個小問題(我不知道它是否是設計模式問題或者是否有解決方案)!Javascript在對象內使用綁定,我怎樣才能訪問這個對象?
以下面爲例;
o.Events = (function() {
"use strict";
function mousedown() {
// Set mousedown boolean
// # How can I change o.Events.mousedown
// For each layer
this.layers.forEach(function(layer) {
// Layer is listening
if (layer.listening && layer.mouse.x && layer.mouse.y) {
console.log("mousedown");
}
});
};
function init(game) {
// Mousedown boolean
this.mousedown = false;
game.element.addEventListener("mousedown", mousedown.bind(game), false);
};
function Events(game) {
// Initialize events
init.call(this, game);
};
return Events;
})();
我怎樣才能改變Events.mousedown
標誌,即使我綁定的遊戲,這樣裏面的功能this
實際上是遊戲?
謝謝
只需引用它的全部範圍。 'o.Events.mousedown = ...' – Madbreaks 2013-04-30 21:28:23
這將有助於看到一點客戶端代碼。也就是說,使用您創建的這個Events對象的代碼。 – Jonah 2013-04-30 21:28:28
在構造函數或mousedown(e)... e.target中使用「that = this」事件' – dandavis 2013-04-30 22:45:49