在函数内部工作时,我无法保持范围。引用正确范围访问soundManager的最佳方法是什么?
未捕获的TypeError:无法设置属性'游戏'未定义的
在此行console.log('SCOPE: ' + MyGame.Game.sound.mute);
$(".audiotoggle").click(function() {
if (MyApp1.gameMuted === false) {
MyApp1.gameMuted = true; //(angular vars)
console.log('MUTE: TRUE (audiotoggle * TRIGGER)');
audtioToggleFunc();
} else {
MyApp1.gameMuted = false; //(angular vars)
console.log('MUTE: FALSE (audiotoggle * TRIGGER)');
audtioToggleFunc();
}
});
var audtioToggleFunc = function () {
console.log('SCOPE: ' + MyGame.Game.sound.mute);
console.log('AUDIO TOGGLE');
if (MyApp1.gameMuted === true) {
MyGame.Game.sound.mute = false;
console.log('MUTE: FALSE (audiotoggle * Function)');
} else {
MyGame.Game.sound.mute = true;
console.log('MUTE: TRUE (audiotoggle * Function)');
}
};
audtioToggleFunc();
答案 0 :(得分:1)
我认为这是一个相当棘手的问题。你应该使用:
$(".audiotoggle").click(
$.proxy(function () {
//your listener code
}, this)
);