superSay.call(this)
為什么會(huì)是People.prototype.say
函數(shù)被調(diào)用?this
? ???? ??? ?????? ??? ?? Student{}
類,這個(gè)你可以在superSay.call(this)
上面加一行console.log(this)
? ?????.
?? ? ??? ???????
?????? ??? ??? ?? ???? new People()
? ??? ????? Instance X
?? ????. new People()
創(chuàng)建的實(shí)例稱為實(shí)例X
。
由于superSay = Student.prototype.say
,因?yàn)樯厦娴?code>Student.prototype = new People();,所以其中Student.prototype
為實(shí)例X
。
所以實(shí)際上superSay
調(diào)用的是實(shí)例X
的say
,而并非People.prototype.say
superSay = Student.prototype.say
?? ?? Student.prototype = new People();
??? Student.prototype
? InstanceX
.
superSay
? People.prototype.say
? ?? Instance X
? say
? ?????.
People.prototype.say
,主要還是原型鏈的問(wèn)題。實(shí)例X
是People類的一個(gè)實(shí)例,所以實(shí)例X
的所有方法會(huì)從People類的原型鏈“繼承”(用繼承這個(gè)詞,但是實(shí)際上JS的原型鏈和繼承還是有些區(qū)別的)。所以實(shí)例X.say
如果沒(méi)有針對(duì)實(shí)例X
重寫say
方法,那么實(shí)例X
的say
就和People.prototype.say
???? ?? ????? ???? ???
superSay.call(this)
這個(gè)里面的call
,只是改變了this
的上下文而已。但是由于superSay
即實(shí)例X.say
,這個(gè)方法里根本沒(méi)有this
,所以this
??
?? ??: ?? say ???? ?? ?? ??? ?? ???? ?????. s——student.prototype——people.prototype——Object? ??? ??? ? ??? ??? ?????. ??? ???? Student.prototype? ?? ? ????. ???? ?? ??? ??? ?? ???? ? ???? ?? ? ????.
????? ??? ???? ????. ?? s.say() - 321? ????, s.jiao() - 456? ?????. ?? ?? Student.prototype.say ???? ????? ??? ??? ???? ? ??? ?????
???? ?? ?? superSay.call(this), ?? superSay? ????, ?? ??? ?????. var superSay = Student.prototype.say ?? console.log(typeof supperSay) ??? ???? ???. , ??? ? ??? ???? ? ??? Student.prototype.say? ?????. var superSay = Student.prototype.say? ?????. ??? ??? ??? ?? People.prototype.say???. ? ?? Elevation? 166-167?????? ?????. ? ?? ???? ??? ? ??? ???? ???? ???? ???? ?? ? ????.
???? ? superSay? ???? ??? ?????. var superSay = Student.prototype.say? ??? ? Student.prototype?? ??? ??? ??? ?? say ???? ?? ?????. ?? ?? ??? ???? People.prototype.say? superSay
? ?????. ?????? ??? ???? ????? ??? ??????
?? ?????? stuSay? ??? ?? ?? function(){alert("hello");}? ?????. stuSay? ?? ???? ?? ?, ??? ???? ?? ? ?? ??? ?????. , ?? ?????. ??? ??? ?? ?? ???? ??? ?????? Rhinoceros ?? ?????...??? ?? ?? ??? ??? ???? ?? ??? ?? ????. ??? ?? ?? ?? ? ?? ?? ??? ???? ????? ?? ???