據我所知,你在JavaScript中聲明的一切,屬於全局對象(除非你聲明的對象的內在的東西,那麼對象將屬於窗口對象,不管你聲明它的內部,對象),因此,在瀏覽器環境中,全局對象是window
。東西我不理解「使用嚴格的」,這
說你聲明: var x = 'hi'
這可以通過訪問:
x
OR
window.x
而且兩者是完全一樣的,RI GHT?那麼,爲什麼,有'use strict'
,從「全局」變量返回this
的時候,我只能如果我指定說功能屬於window
得到window
對象?
function fun() {
'use strict';
return this;
}
fun(); // undefined
window.fun(); // window object
// Aren't both absolutely the same?
而且,爲什麼這個函數返回後undefined
,如果函數應該屬於obj
?
obj = {
method: function() {
'use strict';
function yeah() { // Doesn't this belong to obj?
return this; // It certainly doesn't seem that 'yeah'
} // belongs to window.
return yeah();
}
};
謝謝。
所以'this'嚴格用於對象方法,對嗎?其他任何東西只屬於一個範圍?另外,爲什麼在不帶'window.setTimeout'語法的情況下,在'use strict'方法內調用'setTimeout'會返回'window'對象?它不應該也返回undefined? – ChuckBerry
沒關係,它似乎是這樣的,因爲'setTimeout'在嚴格模式下本身不運行。 – ChuckBerry