深入分析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 屬性又指向了一個對象,這個對象就是prototype對象,請看下圖
a.prototype 包含了2個屬性,一個是constructor ,另外一個是__proto__
這個constructor 就是我們的構(gòu)造函數(shù)a,這個也很容易理解。
那么__proto__ 是什么呢?
這個就涉及到了原型鏈的概念:
每個對象都會在其內(nèi)部初始化一個屬性,就是__proto__,當我們訪問一個對象的屬性 時,如果這個對象內(nèi)部不存在這個屬性,那么他就會去__proto__里找這個屬性,這個__proto__又會有自己的__proto__,于是就這樣 一直找下去。
請看mozzlia 對它對它的描述
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.
不管你信不信,我們來看圖
在后面如果加上 alert(obj.__proto__ === a.prototype) //true
同理,在這里我們來分析出new 運算符做了那些事情
- var obj={}; 也就是說,初始化一個對象obj。
- obj.__proto__=a.prototype;
- a.call(obj);也就是說構(gòu)造obj,也可以稱之為初始化obj。
我們將這個例子改造一些,變得復雜一點。
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');
我們來分析下這個過程
由 var t = new obj('test'); 我們可以得到 t.__proto__ = obj.prototype,但是上面指定obj.prototype =new a('test'); 可以這樣來看下
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,
所以對象t先去找本身是的prototype 是否有test函數(shù),發(fā)現(xiàn)沒有,結(jié)果再往上級找,即 t.__proto__ ,亦即obj.prototype 尋找test函數(shù) ,但是obj.prototype 也沒有這個函數(shù),然后再往上找。即
t.__proto__.__proto__ 找,由于t.__proto__.__proto__ = a.prototype 在 a.prototype 中找到了這個方法,輸出了alert('test')
從這里可以分析得出一個結(jié)論,js中原形鏈的本質(zhì)在于 __proto__
再看看一個列子
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__ 我們來分析下,首先obj是沒有constructor 這個屬性的,但是 obj.__proto__ = a.prototype;就從
a.prototype中尋找,而 a.prototype.constructor 是就a,所有兩者的結(jié)果是一一樣的.
相關(guān)文章
詳解用原生JavaScript實現(xiàn)jQuery的某些簡單功能
本篇文章主要對用原生JavaScript實現(xiàn)jQuery的某些簡單功能進行詳細全面的講解,具有很好的參考價值,需要的朋友一起來看下吧2016-12-12bootstrap fileinput 插件使用項目總結(jié)(經(jīng)驗)
這篇文章主要介紹了bootstrap fileinput 插件使用項目總結(jié),是小編日常碰到的問題及解決方法,需要的朋友可以參考下2017-02-02javascript中parentNode,childNodes,children的應(yīng)用詳解
本篇文章是對javascript中parentNode,childNodes,children的應(yīng)用進行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2013-12-12微信小程序?qū)崿F(xiàn)一張或多張圖片上傳(云開發(fā))
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)一張或多張圖片上傳,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-09-09vue3中Element Plus全局組件配置中文的兩種方案
element-plus組件文字語言默認是英文,需要手動更改一下中文包 ,本文主要介紹了vue3中Element Plus全局組件配置中文的兩種方案,具有一定的參考價值,感興趣的可以了解一下2023-12-12