我有具有兩個功能在其內,並且如我猜到每一個具有用於this
一個不同的值的對象:以下Javascript函數如何影響'this'的值?
custom_controls : {
play_pause : function() {
console.log(this); // object
videoPlayer.getIsPlaying(function (video_is_playing) {
if (video_is_playing) {
console.log(this); // window
videoPlayer.pause(true);
} else {
videoPlayer.play();
}
});
}
},
然後函數被調用這樣的:
custom_controls.play_pause()
我聽說你調用函數的方式表示值爲this
。
所以我的問題是:這裏發生了什麼?我正在使用哪種函數調用?每個人如何影響this
?
我們不知道你是如何調用函數的,因爲它們在其他地方被調用。 –
我不知道你在嘗試什麼,但是如果你想讓'this'實例在其他函數中,那麼'var that = this;'然後在其他函數中使用'that' –
@KendallFrey我已經更新了我的問題,功能。 – shrewdbeans