深入分析js中的constructor和prototype
function a(c){
this.b = c;
this.d =function(){
alert(this.b);
}
}
var obj = new a('test');
alert(typeof obj.prototype);//undefine
alert(typeof a.prototype);//object
從上面的例子可以看出函數(shù)的prototype 屬性又指向了一個(gè)對(duì)象,這個(gè)對(duì)象就是prototype對(duì)象,請(qǐng)看下圖

a.prototype 包含了2個(gè)屬性,一個(gè)是constructor ,另外一個(gè)是__proto__
這個(gè)constructor 就是我們的構(gòu)造函數(shù)a,這個(gè)也很容易理解。
那么__proto__ 是什么呢?
這個(gè)就涉及到了原型鏈的概念:
每個(gè)對(duì)象都會(huì)在其內(nèi)部初始化一個(gè)屬性,就是__proto__,當(dāng)我們?cè)L問(wèn)一個(gè)對(duì)象的屬性 時(shí),如果這個(gè)對(duì)象內(nèi)部不存在這個(gè)屬性,那么他就會(huì)去__proto__里找這個(gè)屬性,這個(gè)__proto__又會(huì)有自己的__proto__,于是就這樣 一直找下去。
請(qǐng)看mozzlia 對(duì)它對(duì)它的描述
When an object is created, its __proto__ property is set to constructing function's prototype property. For example var fred = new Employee(); will cause fred.__proto__ = Employee.prototype;.
This is used at runtime to look up properties which are not declared in the object directly. E.g. when fred.doSomething() is executed and fred does not contain adoSomething, fred.__proto__ is checked, which points to Employee.prototype, which contains a doSomething, i.e. fred.__proto__.doSomething() is invoked.
Note that __proto__ is a property of the instances, whereas prototype is a property of their constructor functions.
不管你信不信,我們來(lái)看圖

在后面如果加上 alert(obj.__proto__ === a.prototype) //true
同理,在這里我們來(lái)分析出new 運(yùn)算符做了那些事情
- var obj={}; 也就是說(shuō),初始化一個(gè)對(duì)象obj。
- obj.__proto__=a.prototype;
- a.call(obj);也就是說(shuō)構(gòu)造obj,也可以稱之為初始化obj。
我們將這個(gè)例子改造一些,變得復(fù)雜一點(diǎn)。
function a(c){
this.b = c;
this.d =function(){
alert(this.b);
}
}
a.prototype.test = function(){
alert(this.b);
}
var obj = function (){}
obj.prototype = new a('test');
obj.prototype.test1 =function(){
alert(22222);
}
var t = new obj('test');
t.test();//alert('test');
我們來(lái)分析下這個(gè)過(guò)程
由 var t = new obj('test'); 我們可以得到 t.__proto__ = obj.prototype,但是上面指定obj.prototype =new a('test'); 可以這樣來(lái)看下
obj.prototype = p, p = new a('test'); p.__proto__ = a.prototype;
那么obj.prototype.__proto__ = a.prototype,由 t.__proto__ = obj.prototype 可以得出 t.__proto__.__proto__ = a.prototype,
所以對(duì)象t先去找本身是的prototype 是否有test函數(shù),發(fā)現(xiàn)沒(méi)有,結(jié)果再往上級(jí)找,即 t.__proto__ ,亦即obj.prototype 尋找test函數(shù) ,但是obj.prototype 也沒(méi)有這個(gè)函數(shù),然后再往上找。即
t.__proto__.__proto__ 找,由于t.__proto__.__proto__ = a.prototype 在 a.prototype 中找到了這個(gè)方法,輸出了alert('test')
從這里可以分析得出一個(gè)結(jié)論,js中原形鏈的本質(zhì)在于 __proto__
再看看一個(gè)列子
function a(c){
this.b = c;
this.d =function(){
alert(this.b);
}
}
var obj = new a('test');
alert(obj.constructor);//function a(){}
alert(a.prototype.constructor);//function a(){}
根據(jù)上面講到的__proto__ 我們來(lái)分析下,首先obj是沒(méi)有constructor 這個(gè)屬性的,但是 obj.__proto__ = a.prototype;就從
a.prototype中尋找,而 a.prototype.constructor 是就a,所有兩者的結(jié)果是一一樣的.
相關(guān)文章
一文詳解如何用原型鏈的方式實(shí)現(xiàn)JS繼承
JavaScript中,每當(dāng)創(chuàng)建一個(gè)對(duì)象,都會(huì)給這個(gè)對(duì)象提供一個(gè)內(nèi)置對(duì)象 [[Prototype]] 。這個(gè)對(duì)象就是原型對(duì)象,[[Prototype]] 的層層嵌套就形成了原型鏈。本文將詳細(xì)講解如何用原型鏈的方式實(shí)現(xiàn)一個(gè) JS 繼承,感興趣的可以了解下2022-04-04
Javascript對(duì)象中關(guān)于setTimeout和setInterval的this介紹
Javascript對(duì)象中關(guān)于setTimeout和setInterval的this介紹,需要的朋友可以參考下2012-07-07
javascript中clipboardData對(duì)象用法詳解
這篇文章主要介紹了javascript中clipboardData對(duì)象用法,詳細(xì)分析了clipboardData對(duì)象的功能及相關(guān)使用技巧,需要的朋友可以參考下2015-05-05
關(guān)于JS與jQuery中的文檔加載問(wèn)題
本文通過(guò)實(shí)例代碼給大家講解了js和jquery中的文檔加載問(wèn)題,感興趣的的朋友一起看看吧2017-08-08
JavaScript回調(diào)函數(shù)callback用法解析
這篇文章主要介紹了JavaScript回調(diào)函數(shù)callback用法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01
讓JavaScript的Alert彈出框失效的方法禁止彈出警告框
彈出框難免會(huì)影響你的心情,所以通過(guò)以下代碼可將Js彈出框屏蔽掉,實(shí)現(xiàn)思路是對(duì)alert方法重寫2014-09-09
countUp.js實(shí)現(xiàn)數(shù)字動(dòng)態(tài)變化效果
這篇文章主要為大家詳細(xì)介紹了countUp.js實(shí)現(xiàn)數(shù)字動(dòng)態(tài)變化效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10
JavaScript更改原始對(duì)象valueOf的方法
這篇文章主要介紹了JavaScript更改原始對(duì)象valueOf的方法,涉及javascript使用自定義valueOf函數(shù)替換掉原始o(jì)bject中valueOf的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03

