1琅翻、js內(nèi)置的構(gòu)造器(函數(shù)對(duì)象)有12個(gè)啊
Number位仁、Object、String方椎、Boolean聂抢、Date、Array棠众、Function琳疏、Math,JSON
每個(gè)對(duì)象都有 proto屬性闸拿,但只有函數(shù)對(duì)象才有 prototype 屬性(函數(shù)對(duì)象有proto和prototype兩個(gè)屬性)
函數(shù)對(duì)象的proto===Function.prototype(所有函數(shù)對(duì)象的proto屬性===Function函數(shù)對(duì)象的prototype屬性)
Object.prototype.proto === null // true
Number.constructor == Function //true
Boolean.__proto__ === Function.prototype // true
Boolean.constructor == Function //true
String.__proto__ === Function.prototype // true
String.constructor == Function //true
// 所有的構(gòu)造器都來(lái)自于Function.prototype空盼,甚至包括根構(gòu)造器Object及Function自身
Object.__proto__ === Function.prototype // true
Object.constructor == Function // true
// 所有的構(gòu)造器都來(lái)自于Function.prototype,甚至包括根構(gòu)造器Object及Function自身
Function.__proto__ === Function.prototype // true
Function.constructor == Function //true
Array.__proto__ === Function.prototype // true
Array.constructor == Function //true
RegExp.__proto__ === Function.prototype // true
RegExp.constructor == Function //true
Error.__proto__ === Function.prototype // true
Error.constructor == Function //true
Date.__proto__ === Function.prototype // true
Date.constructor == Function //true</pre>```
2新荤、相關(guān)原則以及公式
Object.__proto__===Function.prototype //函數(shù)對(duì)象的__proto__屬性===Function函數(shù)對(duì)象的prototype屬性
var a={}
a.constructor===Object //實(shí)例對(duì)象的構(gòu)造函數(shù)屬性===構(gòu)造函數(shù)
對(duì)象.__proto__ === 構(gòu)造函數(shù).prototype
Function.prototype.__proto__ === Object.prototype
構(gòu)造函數(shù).prototype.constructor == 構(gòu)造函數(shù); //原型對(duì)象是構(gòu)造函數(shù)的一個(gè)實(shí)例對(duì)象
對(duì)象.proto == 構(gòu)造函數(shù).prototype;
對(duì)象.constructor == 構(gòu)造函數(shù);
3揽趾、原型對(duì)象(.prototype)——作用(繼承)
共有屬性和方法放在原型對(duì)象里面
.prototype
(Object.prototype).constructor===Object //原型對(duì)象的構(gòu)造函數(shù)===構(gòu)造函數(shù)
在 Object創(chuàng)建對(duì)象的時(shí)候,創(chuàng)建了一個(gè)它的實(shí)例對(duì)象并賦值給它的 prototype迟隅。
原型對(duì)象(Object.prototype)是 構(gòu)造函數(shù)(Object)的一個(gè)實(shí)例但骨。是一個(gè)普通對(duì)象.
原型對(duì)象其實(shí)就是普通對(duì)象(但 Function.prototype 除外,它是函數(shù)對(duì)象智袭,但它很特殊奔缠,他沒(méi)有prototype屬性)
作用:繼承
<pre style="color: rgb(0, 0, 0); font-family: "Courier New"; font-size: 12px; margin: 5px 8px; padding: 5px;"> var Person = function(name){ this.name = name; // tip: 當(dāng)函數(shù)執(zhí)行時(shí)這個(gè) this 指的是誰(shuí)?
};
Person.prototype.getName = function(){ return this.name; // tip: 當(dāng)函數(shù)執(zhí)行時(shí)這個(gè) this 指的是誰(shuí)吼野?
} var person1 = new Person('Mick');
person1.getName(); //Mick</pre>
4校哎、__proto__
對(duì)象.__proto__ === 構(gòu)造函數(shù).prototype
5、函數(shù)對(duì)象
所有函數(shù)對(duì)象的__proto__===Function.protorype,它是一個(gè)空函數(shù)
6、總結(jié)
* 原型和原型鏈?zhǔn)荍S實(shí)現(xiàn)繼承的一種模型闷哆。
* 原型鏈的形成是真正是靠`__proto__` 而非`prototype`
小測(cè)試
1. `person1.__proto__` 是什么腰奋?
2. `Person.__proto__` 是什么?
3. `Person.prototype.__proto__` 是什么抱怔?
4. `Object.__proto__` 是什么劣坊?
5. `Object.prototype__proto__` 是什么?
參考鏈接:http://www.reibang.com/p/dee9f8b14771