2013-11-22 16 views
0

我有具有兩個功能在其內,並且如我猜到每一個具有用於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

+0

我們不知道你是如何調用函數的,因爲它們在其他地方被調用。 –

+0

我不知道你在嘗試什麼,但是如果你想讓'this'實例在其他函數中,那麼'var that = this;'然後在其他函數中使用'that' –

+0

@KendallFrey我已經更新了我的問題,功能。 – shrewdbeans

回答

2

當調用obj.func()this內部功能將等於obj。如果沒有obj,則改爲使用全局對象(window)。或者如果您正在使用Strict Mode,則使用undefined

第一個日誌是你的對象,因爲你調用該函數是這樣的:

custom_controls.play_pause() // custom_controls will be 'this' 

第二日誌窗口,因爲作爲參數傳遞給getIsPlaying功能不叫任何this

videoPlayer.getIsPlaying = function(callback) { 
    callback(); // this inside callback will be window 
} 

使用callapply調用函數時,您可以控制this的值。您可以通過使用bind函數創建一個新的功能,這將永遠有this值設置爲任何你想要的:

videoPlayer.getIsPlaying(function (video_is_playing) { 
     if (video_is_playing) { 

      console.log(this); // my obj 

      videoPlayer.pause(true); 
     } else { 
      videoPlayer.play(); 
     } 
    }.bind(this)); // magic! 

參考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

0

您發現的this對象是您期望的對象,因爲該函數在該對象上運行。

我對您的videoPlayer不熟悉,但由於this的值是「窗口」,我會想象一個視頻播放器是瀏覽器本身的功能,還是B的範圍沒有正確關閉。

0

this指的是「專有」的功能,或者是來自功能的對象是一種方法。

定義基本功能時,「專有」是頁面(或窗口)本身。

當你調用videoPlayer.getIsPlaying它接受一個回調FN您可以檢查的解決方法

0

callback文檔。回調直接調用,如cb(),因此上下文是全局的(窗口)。

要實現在您的對象上下文中發生的回調。您可以使用

var fn = cb.bind(customControl); 
videoPlayer.getIsPlaying(fn);  

作爲一個經驗法則,當一個函數被調用像object.function此項設置爲object。如果直接調用函數,則將this設置爲窗口。 Function.bind在綁定object(此值)之後可以隨參數一起返回函數。

閱讀:MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

1

你的第一個指的是play_pause

您的第二個這個可能是指的是窗口或您的videoPlayer對象。在JavaScript中,閉包和常規函數通常附加到window,並且調用這個返回window。在某些情況下,例如如果附加功能的HTML元素的點擊處理程序,指元素...

element.onclick = function(){ 
    this // -> element 
} 

但通常,如果你只需要創建一個function(),或具有匿名一個像你這樣的thiswindow

function hello(){ 
    this // -> window 
} 
1

每個函數實際上在一個上下文中執行。該上下文表示爲您調用該函數的當前this

給定您的代碼: 如果您致電custom_controls.play_pause(),則表示「將對象custom_controls的字段命名爲play_pause並在對象custom_controls的上下文中執行」。

後來打電話videoPlayer.getIsPlaying()意味着幾乎相同。除了你給它一個回調函數。稍後回調函數的執行方式取決於如何實現videoPlayer.getIsPlaying。 如果我必須我想說getIsPlaying有一個callback.call(window, video_is_playing)在其中的某個地方。

call是javascript中所有函數對象的一種方法。

如果您想在某個回調中引用this,有幾種方法可以解決此「問題」。

var self = this; 
call_me_maybe(function() { 
    console.log(this); //the this that call_me_maybe chose to call your function with 
    console.log(self); //the this from the upper scope 
}); 

,或者如果你不關心這方面call_me_maybe將調用函數的對象:

call_me_maybe((function(){ 
    console.log(this); //the this from the upper scope 
}).bind(this)); 

什麼bind所做的是它返回一個包裹[每功能這將總是在與其綁定的對象的上下文中調用bind也可以爲函數綁定參數以及this對象,創建一種咖喱。

相關問題