[JavaScript] 忍之道解謎(四) - Constructor Invocation
Q. 呼叫 a.bar 的結果是?
這題同樣是考 "Constructor Invocation" 的概念 (詳細可參照此文章),當 function object 與 new 聯用時,會建立一個此 function type 的物件並與其 prototype 產生連結,所以當 a 物件被產生出來時其 this 指向一個新建立出來的物件並非 window,所以當存取 this.foo 時,因為 a 這個 bar type 物件並沒有該屬性,因此答案是 undefined。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
foo = 'jsdc'; | |
function bar(){ | |
this.foo; | |
} | |
a = new bar(); |
留言
張貼留言