Область видимости this в методе push
Здраствуйте, как дать доступ к this анонимной функции внутри метода push?
Пример:
g.module({
create: function(){
this.beep = g.sounds["beep"];
this.vacuum = g.sounds["vacuum"];
this.vacuum.endArr.push(function() {
// другие операции
this.beep.play(); // TypeError: this.beep is undefined
});
}
});
|
function() {
// your code here
}.bind(this);
|
Создать переменную, ссылающуюся на this?
g.module({
create: function(){
this.beep = g.sounds["beep"];
this.vacuum = g.sounds["vacuum"];
var _this = this;
this.vacuum.endArr.push(function() {
// другие операции
_this.beep.play(); // TypeError: this.beep is undefined
});
}
});
|
верное решение - es6 arrow function :)
|
Спасибо большое за ответы, все работает хорошо. Я пробовал с bind'ом ранее, но не работало, а теперь все ок)
|
| Часовой пояс GMT +3, время: 12:16. |