原型
每個(gè)構(gòu)造函數(shù)都有一個(gè)原型對(duì)象铁瞒,原型對(duì)象都包含一個(gè)指向構(gòu)造函數(shù)的指針萎坷,而實(shí)例都包含一個(gè)指向原型對(duì)象的內(nèi)部指針稀余。
實(shí)例對(duì)象有個(gè)屬性[prototype]国葬,叫做(__ proto__),實(shí)例對(duì)象可以通過(guò)這個(gè)屬性訪問(wèn)原型對(duì)象上的屬性和方法
var M = function (name) {
this.name = name
}
var o = new M('o')
o.__proto__// 原型對(duì)象
原型鏈
假如我們讓原型對(duì)象等于另一個(gè)類型的實(shí)例未辆,此時(shí)的原型對(duì)象講包含在一個(gè)指向另一個(gè)原型的指針窟绷,相應(yīng)的,另一個(gè)原型中也包含著一個(gè)指向另一個(gè)構(gòu)造函數(shù)的指針咐柜。這樣層層遞進(jìn)钾麸,就狗證了實(shí)例與原型的鏈條,就是所謂的原型鏈炕桨。
var M = function (name) {
this.name = name
}
var o = new M('o')
M.prototype.say=function(){
console.log(this.name)
}
o.say(); // o
M.__proto__ === Function.prototype; // true
M.prototype.constructor === M; // true
function Parent() {
this.name='parent';
this.home='home';
}
function Child(){
Parent.call(this)
this.type = 'chile'
}
var child= new Child();
console.log(child.home); //home
上例中饭尝,Child類繼承了Parent類,而child是Child類的一個(gè)實(shí)例献宫,當(dāng)我們想打印child.home時(shí)钥平,首先在對(duì)象內(nèi)部尋找該屬性,直至找不到,然后才在該對(duì)象的原型(child.prototype)里去找這個(gè)屬性,Child沒(méi)有home屬性姊途,則從實(shí)例的child.proto(指向其構(gòu)造函數(shù)Child.prototype對(duì)象)對(duì)象中查找涉瘾,此時(shí),Child中也沒(méi)有次屬性,就再向上一級(jí)child.proto.proto也就是Parent的原型對(duì)象上找捷兰,就找到了Parent上的home屬性立叛。
創(chuàng)建對(duì)象的方法
1.字面量
var o1 = {name:'o1'}
var o2 = new Object({name: 'o2'})
2.顯示構(gòu)造函數(shù)
var M = function(name) {this.name = name}
var o3 = new M('o3')
3.Object.create
var p = {name:'p'}
var o4 = Object.create(p)
o4.__proto__ === p //true
instanceof原理
instanceof 運(yùn)算符用來(lái)判斷實(shí)例對(duì)象的proto屬性和構(gòu)造函數(shù)的prototype是否是同一個(gè)引用。instanceof 主要的實(shí)現(xiàn)原理就是只要右邊變量的 prototype 在左邊變量的原型鏈上即可贡茅。
var M = function(name) {this.name = name}
var o5 = new M('o5')
o5 instanceof M; // true
o5 instanceof Object; // true
o5.__proto__ === M.prototype; // true
M.prototype.__proto__ === Object.prototype; // true
o5.__proto__.constructor === M; // true
o5.__proto__.constructor === Object; // false
// 用constructor 判斷構(gòu)造函數(shù)直接生成的實(shí)例對(duì)象更為嚴(yán)謹(jǐn)秘蛇。
new一個(gè)對(duì)象的過(guò)程
function M(name) {
this.name = name
//return this
}
var m = new M('m1');
console.log(m.name);//m1
1.創(chuàng)建一個(gè)新對(duì)象
2.this 指向這個(gè)新對(duì)象
3.執(zhí)行代碼其做,即對(duì) this 賦值
4.返回 this(若返回值不為對(duì)象時(shí))
// 模擬new運(yùn)算符的工作原理
var new2 = function(fun) {
var o = Object.create(fun.prototype)
var k = fun.call(o)
if(typeof k === 'object') {
return k;
} else {
return o;
}
}
var o6 = new2(M)
o6.intanceof(M) //true
o6.__proto__.constructor === M //true