Stories

Detail Return Return

Vue與VueComponent的關係 - Stories Detail

結論:VueComponent.prototype.__proto__ === Vue.prototype

Vue與VueComponent的關係如圖所示:
image.png
(圖一)

前提知識:
每個函數function都有一個prototype屬性,即顯式原型(屬性),它默認指向Object空對象,
每個實例對象都有一個__proto__屬性,即稱隱式原型(屬性)。

關係圖分析:
當我們創建一個vue實例對象vm的時候,
則Vue.prototype === vm.__proto__,都指向Vue的原型對象。
當我們創建一個組件實例對象vc的時候,
則VueComponent.prototype === vc.__proto__,都指向VueComponent.prototype的原型對象。

Vue和VueComponent最終本該都指向Object原型對象(如圖二黃實線),可VueComponent把原本指向Object的原型對象改到指向Vue原型對象(如一圖黃虛線),即VueComponent.prototype.__proto__ === Vue.prototype,目的是為了組件實例對象(vc)可以訪問到Vue原型上的屬性和方法
image.png
(圖二)

Add a new Comments

Some HTML is okay.