首先要記撞看铡:
①:__proto__
和constructor
是對象獨(dú)有的祷嘶;
②:prototype
是函數(shù)獨(dú)有的;
③: 函數(shù) 也是對象绳姨;
④:實(shí)例對象的 __proto__
就是構(gòu)造該實(shí)例對象的 構(gòu)造函數(shù) 的原型(prototype
)愈诚;
原型(prototype
)也是對象她按,它也有 __proto__
;
1. 原型鏈?zhǔn)鞘裁?/h6>
簡單理解來就是:
當(dāng)訪問對象上的一個(gè)屬性時(shí)牛隅,該對象上不存在就會向它的 __proto__
上去查找,如果它的 __proto__
上不存在尤溜,就會向 __proto__
的 __proto__
上去找倔叼,如此反復(fù)向上查找,直到頂端 null宫莱,如此形成一條鏈丈攒,這條鏈就叫 原型鏈。
2. __proto__
和 prototype
關(guān)系
__proto__
:是實(shí)例對象上的 一個(gè)屬性 (姑且稱為隱式原型)
prototype
:叫原型授霸,是一個(gè)對象巡验,它是構(gòu)造某個(gè) 實(shí)例對象的 構(gòu)造函數(shù) 的原型。
__proto__
是構(gòu)造該 實(shí)例對象 的 構(gòu)造函數(shù) 的原型(prototype
)
下面參照例子都是簡單列出幾項(xiàng)對比:
// prototype 是一個(gè)對象
typeof Object.prototype // 結(jié)果:'object'
typeof Array.prototype // 結(jié)果:'object'
typeof Function.prototype // 結(jié)果:'object'
typeof Number.prototype // 結(jié)果:'object'
// ···
// ···
{}.__proto__ === Object.prototype // 結(jié)果為 true
[].__proto__ === Array.prototype // 結(jié)果為 true
let num = 123
num.__proto__ === Number.prototype // 結(jié)果為 true
function fun() {}
fun.__proto__ === Function.prototype // 結(jié)果為 true
3. __proto__
原型鏈的極致就是 null
// prototype 是一個(gè)對象碘耳,是對象就有 __proto__ 屬性
{}.__proto__ === Object.prototype
Object.prototype.__proto__ === null // 結(jié)果 true
// ---
// prototype 是一個(gè)對象显设,是對象就有 __proto__ 屬性
[].__proto__ === Array.prototype
Array.prototype.__proto__ === Object.prototype // 結(jié)果 true
Object.prototype.__proto__ === null // 結(jié)果 true
// ---
let num = 123
num.__proto__ === Number.prototype
Number.prototype.__proto__ === Object.prototype // 結(jié)果 true
Object.prototype.__proto__ === null // 結(jié)果 true
// ---
function fun() {}
fun.__proto__ === Function.prototype
Function.prototype.__proto__ === Object.prototype // 結(jié)果 true
Object.prototype.__proto__ === null // 結(jié)果 true
4. 原型prototype
Object,Array辛辨,Number捕捂,F(xiàn)unction 都是 函數(shù),構(gòu)造函數(shù)是一種特殊的 函數(shù)
函數(shù)上有原型斗搞,constructor指攒,等一些列定義的屬性方法。
Function 構(gòu)造函數(shù)的 原型(prototype
)是所有函數(shù)的 __proto__
// Object僻焚,Array允悦,Number,F(xiàn)unction 等都是 構(gòu)造函數(shù)虑啤,函數(shù)也是對象隙弛,是對象就有 __proto__;
// __proto__ 是構(gòu)造 某實(shí)例對象 的 構(gòu)造函數(shù) 的原型
Object.__proto__ === Function.prototype
Array.__proto__ === Function.prototype
Number.__proto__ === Function.prototype
// Function 是一個(gè)構(gòu)造函數(shù),構(gòu)造函數(shù)是一種特殊的函數(shù)狞山。
// Function 構(gòu)造函數(shù)的 原型(prototype)是所有函數(shù)的 __proto__全闷,因此
Function.__proto__ === Function.prototype
// 下面這個(gè)不要跟上面的理解混了
Function.prototype.__proto__ === Object.prototype
5.constructor
constructor
指向該對象的構(gòu)造函數(shù)。每個(gè)對象上都有構(gòu)造函數(shù)(有的 本身擁有铣墨,有的通過繼承而來室埋,繼承的需通過 .proto 來表現(xiàn))。
function Fun() {}
let ff = new Fun
ff.constructor === Fun // true伊约; ff 的 constructor 通過繼承而來
Fun.prototype.constructor === Fun // true
// 即
ff.__proto__.constructor === Fun //true
Fun.constructor === Function // true;Fun 的 constructor 通過繼承而來
Function.prototype.constructor === Function // true
Object.prototype.constructor === Object // true
Object.constructor === Function // true孕蝉;Object 的 constructor 通過繼承而來
所有的 函數(shù) 和 對象屡律,最終都是由構(gòu)造函數(shù) Function
構(gòu)造而來,所以 constructor
的終點(diǎn)就是 Function
降淮。
如果超埋,從頭理解下來的話搏讶,理論上啊,我只是說理論上應(yīng)該就懂了這個(gè)關(guān)系了霍殴。
一定要多輸出查看
以上皆為個(gè)人整理媒惕,如有錯(cuò)誤,請指正来庭,謝謝 ??