2013-01-10 55 views

回答

2

每個功能有prototype屬性(即使你沒有把它定義),prototype對象有唯一的財產constructor(指向函數本身)。因此,在你做了Student.prototype = new Person();constructor屬性prototype正指向Person函數,所以你需要重置它。

你不應該認爲prototype.constructor是神奇的東西,它只是一個指向函數的指針。即使你跳過Student.prototype.constructor = Student;new Student();也會按照它的原樣工作。

constructor該屬性是有用的,例如,在下列情況下(當你需要克隆的對象,但不知道究竟是什麼功能創造了它):

var st = new Student(); 
... 
var st2 = st.constructor(); 

所以最好以確保prototype.constructor()是正確的。

+0

'var st2 = st.constructor();'錯過了'new'關鍵字。它應該是'var st2 = new st.constructor();' – golem

相關問題