1.什么是原型鏈酒奶?
每個(gè)構(gòu)造函數(shù)都有原型對(duì)象;每個(gè)對(duì)象都會(huì)有構(gòu)造函數(shù)荠卷;每個(gè)構(gòu)造函數(shù)的原型都是一個(gè)對(duì)象列林;那么這個(gè)原型對(duì)象也會(huì)有構(gòu)造函數(shù)瑞你;那么這個(gè)原型對(duì)象的構(gòu)造函數(shù)也會(huì)有原型對(duì)象;這樣就會(huì)形成一個(gè)鏈?zhǔn)降慕Y(jié)構(gòu)希痴,稱(chēng)為原型鏈者甲。
2.原型鏈結(jié)構(gòu)的基本形式
function Person(name){ this.name = name; }
var p = new Person();
//p ---> Person.prototype --->Object.prototype---->null·
3.屬性搜索原則:
a.當(dāng)訪問(wèn)一個(gè)對(duì)象的成員的時(shí)候,會(huì)現(xiàn)在自身找有沒(méi)有,如果找到直接使用砌创,
b.如果沒(méi)有找到虏缸,則去當(dāng)前對(duì)象的原型對(duì)象中去查找,如果找到了直接使用嫩实,
c.如果沒(méi)有找到刽辙,繼續(xù)找原型對(duì)象的原型對(duì)象,如果找到了甲献,直接使用
d.如果沒(méi)有找到宰缤,則繼續(xù)向上查找,直到Object.prototype晃洒,如果還是沒(méi)有慨灭,就報(bào)錯(cuò)
4.原型繼承概念
通過(guò)修改原型鏈結(jié)構(gòu)實(shí)現(xiàn)的繼承,就叫做原型繼承球及。
5.復(fù)雜的原型鏈
//動(dòng)物--->人---->老師---->壞老師
function Animal(){
this.gender = "male";
}
Human.prototype = new Animal();
Human.prototype.constructor = Human;
function Human(){
this.actionWay = "走路";
}
Teacher.prototype = new Human();
Teacher.prototype.constructor = Teacher;
function Teacher(){
this.skill = "教書(shū)";
}
BadTeacher.prototype = new Teacher();
BadTeacher.prototype.constructor = BadTeacher;
function BadTeacher(){
this.name = "呂超";
}
var t = new BadTeacher();
console.log(t);
6.Object.prototype的成員
①.constructor:
原型對(duì)象內(nèi)的一個(gè)屬性氧骤,指向該原型對(duì)象相關(guān)聯(lián)的構(gòu)造函數(shù)。
②.hasOwnProperty:
一個(gè)方法吃引,用來(lái)判斷對(duì)象本身(不包含原型)是否擁有某個(gè)屬性语淘。
function Person(){ this.name = "王九" } Person.prototype.name = "張三";
var p = new Person();
console.log(p.name);
console.log(p.hasOwnProperty("__proto__"));
③.propertyIsEnumerable:
a. 判斷屬性是否屬于對(duì)象本身
b. 判斷屬性是否可以被遍歷
console.log(p.propertyIsEnumerable("name"));
④.將對(duì)象轉(zhuǎn)換成字符串
var o = {}; console.log(o.toString()); console.log(o.toLocaleString());
var now = new Date();
console.log(now.toString());
console.log(now.toLocaleString());
⑤.valueOf:
獲取當(dāng)前對(duì)象的值
function Person(){
}
var p = new Person();
//在對(duì)象參與運(yùn)算的時(shí)候
//1.默認(rèn)的會(huì)先去調(diào)用對(duì)象的valueOf方法,
//2.如果valueOf獲取到的值际歼,無(wú)法進(jìn)行運(yùn)算 惶翻,就去去調(diào)用p的toString方法 最終做的就是字符串拼接的工作
console.log( 1 + p);
⑥ proto:
a.原型對(duì)象對(duì)象中的屬性
b.可以使用 對(duì)象.proto去訪問(wèn)原型對(duì)象