關(guān)于prototype的幾個問題
如果把 Student.prototype=Object.create(Person.prototype) 改成 Student.prototype=Person.prototype
1.修改后Student和Person的prototype就都變成 了Student ? ,Bosn老師說的是Person.prototype被污染了,這句話怎么理解??
2.Bosn 老師 ?我看到constructor里面的prototype有什么意義?
如果把 Student.prototype=Object.create(Person.prototype) 改成 Student.prototype=Person.prototype
1.修改后Student和Person的prototype就都變成 了Student ? ,Bosn老師說的是Person.prototype被污染了,這句話怎么理解??
2.Bosn 老師 ?我看到constructor里面的prototype有什么意義?
2016-02-04
舉報
2016-02-14
首先你要明白什么時候繼承,比如你繼承我的財產(chǎn),我的財產(chǎn)就是你的,而你自己的還是你自己的。
同樣,Student.prototype=Object.create(Person.prototype)這條語句是Student.prototype繼承了Person.prototype的屬性。再繼承屬性之后,Student還可以設置自己的屬性(Person不會擁有)。
而Student.prototype=Person.prototype會導致在Student.prototype繼承了Person.prototype的屬性之后,Student.prototype設置自己的屬性,Person.prototype也會擁有該屬性。從而導致了Person.prototype被污染了。
一般來說,一個對象的prototype里會有constructor和__proto__兩個內(nèi)置屬性,constructor一般是該函數(shù)對象(數(shù)組類型),而__proto__就是原型鏈咯