我知道,頂級的功能是window
對象的成員,這是一個事實,即this
裏面他們指window
一致。任何對象的非頂級函數成員?
但是對於非頂級功能,this
仍然爲window
,但它們不是window
的成員!
下面是一些代碼:
function topLevel1() {
alert(this)
}
function topLevel2() {
function inner() {
alert(this)
}
inner()
}
topLevel1() // alerts DOMWindow
alert(window.topLevel1) // alerts text of topLevel1, as expected
topLevel2() // again, alerts DOMWindow
alert(window.inner) // undefined
alert(window.topLevel2.inner) // undefined
- 如果
inner
既不是的window
也不是topLevel2
成員,其成員是什麼呢? - 它怎麼可能是它
this
是window
,但它不是window
的成員? - 是不是在Javascript中的法律,如果
this == owner
,那麼當前的方法是通過owner.
調用? (除特殊情況下,像構造函數,apply
,call
等)
比ks,現在對我來說很明顯。 – Fixpoint 2012-03-22 00:30:11
雖然我不明白爲什麼JS不會允許使用'obj.f'語法來調用具有'this == obj'的命名函數'f'。 – Fixpoint 2012-03-22 00:31:52
無視這一點,我明白了,'.'只適用於會員。 – Fixpoint 2012-03-22 00:32:14