深入分析js中的constructor和prototype

來源:互聯網
上載者:User

我們在定義函數的時候,函數定義的時候函數本身就會預設有一個prototype的屬性,而我們如果用new 運算子來產生一個對象的時候就沒有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

從上面的例子可以看出函數的prototype 屬性又指向了一個對象,這個對象就是prototype對象,請看

a.prototype 包含了2個屬性,一個是constructor ,另外一個是__proto__

這個constructor  就是我們的建構函式a,這個也很容易理解。

那麼__proto__ 是什麼呢?

這個就涉及到了原型鏈的概念:

  每個對象都會在其內部初始化一個屬性,就是__proto__,當我們訪問一個對象的屬性 時,如果這個對象內部不存在這個屬性,那麼他就會去__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 運算子做了那些事情

  1.  var obj={}; 也就是說,初始化一個對象obj。
  2. obj.__proto__=a.prototype;
  3.  a.call(obj);也就是說構造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函數,發現沒有,結果再往上級找,即 t.__proto__ ,亦即obj.prototype 尋找test函數 ,但是obj.prototype 也沒有這個函數,然後再往上找。即

t.__proto__.__proto__ 找,由於t.__proto__.__proto__ = a.prototype 在 a.prototype 中找到了這個方法,輸出了alert('test')

從這裡可以分析得出一個結論,js中原形鏈的本質在於 __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(){}

根據上面講到的__proto__ 我們來分析下,首先obj是沒有constructor 這個屬性的,但是 obj.__proto__ = a.prototype;就從

a.prototype中尋找,而 a.prototype.constructor 是就a,所有兩者的結果是一一樣的.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.