欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Js原型鏈constructor prototype __proto__屬性實例詳解

 更新時間:2023年10月20日 09:15:08   作者:油墨香^_^  
這篇文章主要介紹了Js原型鏈constructor prototype __proto__屬性實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

導讀

__proto__(實際原型)和prototype(原型屬性)不一樣?。?!

constructor屬性(原型對象中包含這個屬性,實例當中也同樣會繼承這個屬性)

prototype屬性(constructor.prototype原型對象)

__proto__屬性(實例指向原型對象的指針)

首先弄清楚幾個概念:

什么是對象?

若干屬性的集合

什么是原型?

原型是一個對象,其他對象可以通過它實現(xiàn)繼承。

哪些對象有原型?

所有的對象在默認情況下都有一個原型,因為原型本身也是對象,所以每個原型自身又有一個原型(只有一種例外,默認的對象原型在原型鏈的頂端)

任何一個對象都可以成為原型

接下來就是最核心的內(nèi)容:

constructor 屬性

constructor屬性始終指向創(chuàng)建當前對象的構(gòu)造函數(shù)。

var arr=[1,2,3];

console.log(arr.constructor); //輸出 function Array(){}
var a={};
console.log(arr.constructor);//輸出 function Object(){}
var bool=false;
console.log(bool.constructor);//輸出 function Boolean(){}
var name="hello";
console.log(name.constructor);//輸出 function String(){}
var sayName=function(){}
console.log(sayName.constrctor)// 輸出 function Function(){}
//接下來通過構(gòu)造函數(shù)創(chuàng)建instance
function A(){}
var a=new A();
console.log(a.constructor); //輸出 function A(){}

以上部分即解釋了任何一個對象都有constructor屬性,指向創(chuàng)建這個對象的構(gòu)造函數(shù)

prototype屬性

注意:prototype是每個函數(shù)對象都具有的屬性,被稱為原型對象,而__proto__屬性才是每個對象才有的屬性。一旦原型對象被賦予屬性和方法,那么由相應的構(gòu)造函數(shù)創(chuàng)建的實例會繼承prototype上的屬性和方法。

var arr=[1,2,3];
    console.log(arr.constructor); //輸出 function Array(){}
    var a={};
    console.log(arr.constructor);//輸出 function Object(){}
    var bool=false;
    console.log(bool.constructor);//輸出 function Boolean(){}
    var name="hello";
    console.log(name.constructor);//輸出 function String(){}
    var sayName=function(){}
    console.log(sayName.constrctor)// 輸出 function Function(){}
    
    //接下來通過構(gòu)造函數(shù)創(chuàng)建instance
    function A(){}
    var a=new A();
    console.log(a.constructor); //輸出 function A(){}

constructor屬性和prototype屬性

每個函數(shù)都有prototype屬性,而這個prototype的constructor屬性會指向這個函數(shù)。

function Person(name){
        this.name=name;
    }
    Person.prototype.sayName=function(){
        console.log(this.name);
    }
    var person=new Person("xl");
    console.log(person.constructor); //輸出 function Person(){}
    console.log(Person.prototype.constructor);//輸出 function Person(){}
    console.log(Person.constructor); //輸出 function Function(){}

如果我們重寫(重新定義)這個Person.prototype屬性,那么constructor屬性的指向就會發(fā)生改變了。

  Person.prototype={
        sayName:function(){
            console.log(this.name);
        }
    }
    console.log(person.constructor==Person); //輸出 false (這里為什么會輸出false后面會講)
    console.log(Person.constructor==Person); //輸出 false
    console.log(Person.prototype.constructor);// 輸出 function Object(){}  
    //這里為什么會輸出function Object(){}
    //還記得之前說過constructor屬性始終指向創(chuàng)建這個對象的構(gòu)造函數(shù)嗎?
    Person.prototype={
        sayName:function(){
            console.log(this.name);
        }
    }
    //這里實際上是對原型對象的重寫:
    Person.prototype=new Object(){
        sayName:function(){
            console.log(this.name);
        }
    }
    //看到了吧?,F(xiàn)在Person.prototype.constructor屬性實際上是指向Object的。
    //那么我如何能將constructor屬性再次指向Person呢?
    Person.prototype.constructor=Person;

接下來解釋為什么,看下面的例子

function Person(name){
        this.name = name;
    } 
    var personOne=new Person("xl"); 
    Person.prototype = {
        sayName: function(){
            console.log(this.name);
        }
    };
    var personTwo = new Person('XL');
    console.log(personOne.constructor == Person); //輸出true
    console.log(personTwo.constructor == Person); //輸出false   
    //大家可能會對這個地方產(chǎn)生疑惑?為何會第二個會輸出false,personTwo不也是由Person創(chuàng)建的嗎?這個地方應該要輸出true?。?
    //這里就涉及到了js里面的原型繼承
    //這個地方是因為person實例繼承了Person.prototype原型對象的所有的方法和屬性,包括constructor屬性。當Person.prototype的constructor發(fā)生變化的時候,相應的person實例上的constructor屬性也會發(fā)生變化。所以第二個會輸出false;
    //當然第一個是輸出true,因為改變構(gòu)造函數(shù)的prototype屬性是在personOne被創(chuàng)建出來之后。

__proto__和prototype屬性

同樣拿上面的代碼來解釋:

function Person(name){
        this.name=name;
    }
    Person.prototype.sayName=function(){
        console.log(this.name);
    }
    var person=new Person("xl");
    person.sayName(); //輸出 "xl"
    //constructor : Person
    //instance : person
    //prototype : Person.prototype
//constructor : Person
//instance : person
//prototype : Person.prototype

首先給構(gòu)造函數(shù)的原型對象Person.prototype賦給sayName方法,由構(gòu)造函數(shù)Person創(chuàng)建的實例person會繼承原型對象上的sayName方法。

為什么會繼承原型對象的方法?

因為ECMAscript的發(fā)明者為了簡化這門語言,同時又保持繼承性,采用了鏈式繼承的方法。

由constructor創(chuàng)建的每個instance都有個__proto__屬性,它指向constructor.prototype。那么constrcutor.prototype上定義的屬性和方法都會被instance所繼承。

function Person(name){
        this.name=name;
    }
    Person.prototype.sayName=function(){
        console.log(this.name);
    }
    var personOne=new Person("a");
    var personTwo=new Person("b");
    personOne.sayName(); // 輸出  "a"
    personTwo.sayName(); //輸出 "b"
    console.log(personOne.__proto__==Person.prototype); // true
    console.log(personTwo.__proto__==Person.prototype); // true
    console.log(personOne.constructor==Person); //true
    console.log(personTwo.constructor==Person); //true
    console.log(Person.prototype.constructor==Person); //true
    console.log(Person.constructor); //function Function(){}
    console.log(Person.__proto__.__proto__); // Object{}

以上就是Js原型鏈constructor prototype __proto__屬性實例詳解的詳細內(nèi)容,更多關于Js 原型鏈屬性的資料請關注腳本之家其它相關文章!

相關文章

  • 淺談 JavaScript 沙箱Sandbox

    淺談 JavaScript 沙箱Sandbox

    在計算機安全中,沙箱(Sandbox)是一種用于隔離正在運行程序的安全機制,通常用于執(zhí)行未經(jīng)測試或不受信任的程序或代碼,它會 為待執(zhí)行的程序創(chuàng)建一個獨立的執(zhí)行環(huán)境,內(nèi)部程序的執(zhí)行不會影響到外部程序的運行,下文我們來介紹一個“瀏覽器世界”的沙箱
    2021-10-10
  • 微信小程序中頂部導航欄的實現(xiàn)代碼

    微信小程序中頂部導航欄的實現(xiàn)代碼

    這篇文章主要介紹了微信小程序中頂部導航欄的實現(xiàn)代碼的相關資料,需要的朋友可以參考下
    2017-03-03
  • ECharts圖表使用及異步加載的特性示例詳解

    ECharts圖表使用及異步加載的特性示例詳解

    這篇文章主要為大家介紹了ECharts圖表使用及異步加載的特性詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • 微信小程序 詳解下拉加載與上拉刷新實現(xiàn)方法

    微信小程序 詳解下拉加載與上拉刷新實現(xiàn)方法

    這篇文章主要介紹了微信小程序 詳解下拉加載與上拉刷新實現(xiàn)方法的相關資料,這里介紹了兩種實現(xiàn)方法,需要的朋友可以參考下
    2017-01-01
  • 微信小程序 scroll-view組件實現(xiàn)列表頁實例代碼

    微信小程序 scroll-view組件實現(xiàn)列表頁實例代碼

    這篇文章主要介紹了微信小程序 scroll-view組件實現(xiàn)列表頁實例代碼的相關資料,scroll-view組件介紹scroll-view是微信小程序提供的可滾動視圖組件,其主要作用是可以用來做手機端經(jīng)常會看到的上拉加載 ,需要的朋友可以參考下
    2016-12-12
  • JS 里為什么會有 this

    JS 里為什么會有 this

    這篇文章主要介紹了JS 里為什么會有 this,文章主要從語言創(chuàng)造者(JS 之父的角度)來思考 this,我之前那篇講 this 的文章是從使用者的角度寫的,需要的朋友可以參考一下
    2021-10-10
  • ChatGPT前端編程秀之別拿編程語言不當語言

    ChatGPT前端編程秀之別拿編程語言不當語言

    這篇文章主要為大家介紹了ChatGPT前端編程秀之教你別拿編程語言不當語言,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03
  • 利用javaScript處理常用事件詳解

    利用javaScript處理常用事件詳解

    這篇文章主要介紹了利用javaScript處理常用事件詳解,文章有非常詳細的代碼實踐,對學習js的小伙伴們有一定的參考價值,需要的朋友可以參考下
    2021-04-04
  • 一文詳解js基本類型與引用類型的區(qū)別

    一文詳解js基本類型與引用類型的區(qū)別

    這篇文章主要為大家介紹了js基本類型與引用類型的區(qū)別詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-06-06
  • 微信小程序中的onLoad詳解及簡單實例

    微信小程序中的onLoad詳解及簡單實例

    這篇文章主要介紹了微信小程序中的onLoad詳解及簡單實例的相關資料,需要的朋友可以參考下
    2017-04-04

最新評論