<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆 - 12, 文章 - 0, 評(píng)論 - 22, 引用 - 0
    數(shù)據(jù)加載中……

    Js中Prototype、__proto__、Constructor、Object、Function關(guān)系介紹

    一    Prototype、__proto__與Object、Function關(guān)系介紹

            Function、Object:Js自帶的函數(shù)對(duì)象。
            prototype,每一個(gè)函數(shù)對(duì)象都有一個(gè)顯示的prototype屬性,它代表了對(duì)象的原型(Function.prototype函數(shù)對(duì)象是個(gè)例外,沒(méi)有prototype屬性)。
            __proto__:每個(gè)對(duì)象都有一個(gè)名為_(kāi)_proto__的內(nèi)部隱藏屬性,指向于它所對(duì)應(yīng)的原型對(duì)象(chrome、firefox中名稱(chēng)為_(kāi)_proto__,并且可以被訪問(wèn)到)。原型鏈正是基于__proto__才得以形成(note:不是基于函數(shù)對(duì)象的屬性prototype)。
           關(guān)于上面提到的函數(shù)對(duì)象,我們來(lái)看以下例子,來(lái)說(shuō)明:
            var o1 = {};
            var o2 =new Object();
            
            function f1(){}
            var f2 = function(){}
            var f3 = new Function('str','console.log(str)');
        
            f3('aabb');   // aabb
            console.log('typeof Object:'+typeof Object);            //function
            console.log('typeof Function:'+typeof Function);        //function
            console.log('typeof o1:'+typeof o1);   //object
            console.log('typeof o2:'+typeof o2);   //object
            console.log('typeof f1:'+typeof f1);   //function
            console.log('typeof f2:'+typeof f2);   //function
            console.log('typeof f3:'+typeof f3);   //function
    • 通常我們認(rèn)為o1、o2是對(duì)象,即普通對(duì)象;f1、f2、f3為函數(shù)。
    • 但是其實(shí)函數(shù)也是對(duì)象,是由Function構(gòu)造的,
    • f3這種寫(xiě)法就跟對(duì)象的創(chuàng)建的寫(xiě)法一樣。f1、f2最終也都像f3一樣是有Function這個(gè)函數(shù)構(gòu)造出來(lái)的
    • f1、f2、f3為函數(shù)對(duì)象,F(xiàn)unction跟Object本身也是函數(shù)對(duì)象。
           Js中每個(gè)對(duì)象(null除外)都和另一個(gè)對(duì)象相關(guān)聯(lián),通過(guò)以下例子跟內(nèi)存效果圖來(lái)分析Function、Object、Prototype、__proto__對(duì)象間的關(guān)系。

        function Animal(){
            
        }
        var  anim = new Animal();
        
        console.log('***********Animal anim proto*****************');
        console.log('typeof Animal.prototype:' +typeof Animal.prototype);  //object 
        console.log('anim.__proto__===Animal.prototype:'+(anim.__proto__===Animal.prototype));  //true
        console.log('Animal.__proto__===Function.prototype:'+(Animal.__proto__===Function.prototype));  //true
        console.log('Animal.prototype.__proto__===Object.prototype:'+(Animal.prototype.__proto__===Object.prototype));  //true
        
        console.log('***********Function proto*****************');
        console.log('typeof Function.prototype:'+typeof Function.prototype);  //function
        console.log('typeof Function.__proto__:'+typeof Function.__proto__);  //function
        console.log('typeof Function.prototype.prototype:'+typeof Function.prototype.prototype); //undefined
        console.log('typeof Function.prototype.__proto__:'+typeof Function.prototype.__proto__);   //object
        console.log('Function.prototype===Function.__proto__:'+(Function.prototype===Function.__proto__)); //true

        console.log('***********Object proto*****************');
        console.log('typeof Object.prototype:'+typeof Object.prototype);  //object
        console.log('typeof Object.__proto__:'+typeof Object.__proto__);  //function
        console.log('Object.prototype.prototype:'+Object.prototype.prototype);  //undefied
        console.log('Object.prototype.__proto__===null:'+(Object.prototype.__proto__===null));  //null

        console.log('***********Function Object  proto關(guān)系*****************');
        console.log('Function.prototype===Object.__proto__:'+(Function.prototype===Object.__proto__));   //true
        console.log('Function.__proto__===Object.__proto__:'+(Function.__proto__===Object.__proto__));   //true
        console.log('Function.prototype.__proto__===Object.prototype:'+(Function.prototype.__proto__===Object.prototype));   //true

        /********************* 系統(tǒng)定義的對(duì)象Array、Date ****************************/
        console.log('**************test Array、Date****************');      
        var array = new Array();
        var date = new Date();
        console.log('array.__proto__===Array.prototype:'+(array.__proto__===Array.prototype));   //true
        console.log('Array.__proto__===Function.prototype:'+(Array.__proto__===Function.prototype));  //true
        console.log('date.__proto__===Date.prototype:'+(date.__proto__===Date.prototype));    //true
        console.log('Date.__proto__===Function.prototype:'+(Date.__proto__===Function.prototype));     //true

    Function、Object、Prototype、__proto__內(nèi)存關(guān)系圖


            上面的內(nèi)存圖跟堆棧結(jié)構(gòu)可以參照文章Javascript_01_理解內(nèi)存分配
            堆區(qū)圖說(shuō)明:
     
            Function.prototype函數(shù)對(duì)象圖內(nèi)部表示prototype屬性的紅色虛框,只是為了說(shuō)明這個(gè)屬性不存在。

            通過(guò)上圖Function、Object、Prototype關(guān)系圖中,可以得出一下幾點(diǎn):
    1. 所有對(duì)象所有對(duì)象,包括函數(shù)對(duì)象的原型鏈最終都指向了Object.prototype,而Object.prototype.__proto__===null,原型鏈至此結(jié)束。
    2. Animal.prototype是一個(gè)普通對(duì)象。
    3. Object是一個(gè)函數(shù)對(duì)象,也是Function構(gòu)造的,Object.prototype是一個(gè)普通對(duì)象。
    4. Object.prototype.__type__指向null。
    5. Function.prototype是一個(gè)函數(shù)對(duì)象,前面說(shuō)函數(shù)對(duì)象都有一個(gè)顯示的prototype屬性,但是Function.prototype卻沒(méi)有prototype屬性,即Function.prototype.prototype===undefined,所有Function.prototype函數(shù)對(duì)象是一個(gè)特例,沒(méi)有prototype屬性。
    6. Object雖是Function構(gòu)造的一個(gè)函數(shù)對(duì)象,但是Object.prototype沒(méi)有指向Function.prototype,即Object.prototype!==Function.prototype。

    二    Prototype跟Constructor關(guān)系
    介紹
             在 JavaScript 中,每個(gè)函數(shù)對(duì)象都有名為“prototype”的屬性(上面提到過(guò)Function.prototype函數(shù)對(duì)象是個(gè)例外,沒(méi)有prototype屬性),用于引用原型對(duì)象。此原型對(duì)象又有名為“constructor”的屬性,它反過(guò)來(lái)引用函數(shù)本身。這是一種循環(huán)引用(i.e. Animal.prototype.constructor===Animal)。
            通過(guò)以下例子跟內(nèi)存效果圖來(lái)分析Prototype、constructor間的關(guān)系。
        console.log('**************constructor****************'); 

        console.log('anim.constructor===Animal:'+(anim.constructor===Animal))    ;    //true
        console.log('Animal===Animal.prototype.constructor:'+(Animal===Animal.prototype.constructor))    ;    //true
        console.log('Animal.constructor===Function.prototype.constructor:'+(Animal.constructor===Function.prototype.constructor));   //true
        console.log('Function.prototype.constructor===Function:'+(Function.prototype.constructor===Function));    //true
        console.log('Function.constructor===Function.prototype.constructor:'+(Function.constructor===Function.prototype.constructor));    //true

        console.log('Object.prototype.constructor===Object:'+(Object.prototype.constructor===Object));    //true
        console.log('Object.constructor====Function:'+(Object.constructor===Function));    //true

     prototype、constructor內(nèi)存關(guān)系圖(在Function、Object、Prototype關(guān)系圖上加入constructor元素):


            上圖中,紅色箭頭表示函數(shù)對(duì)象的原型的constructor所指向的對(duì)象。
    1. 注意Object.constructor===Function;本身Object就是Function函數(shù)構(gòu)造出來(lái)的        
    2. 如何查找一個(gè)對(duì)象的constructor,就是在該對(duì)象的原型鏈上尋找碰到的第一個(gè)constructor屬性所指向的對(duì)象。
    參考:
    http://www.libuchao.com/2012/05/14/prototypes-in-javascript/ (JavaScript 的原型對(duì)象 Prototype)
    http://rockyuse.iteye.com/blog/1426510 (理解js中的原型鏈,prototype與__proto__的關(guān)系)



    posted on 2013-10-20 23:47 heavensay 閱讀(32440) 評(píng)論(13)  編輯  收藏 所屬分類(lèi): web-front

    評(píng)論

    # re: Js中Prototype、__proto__、Constructor、Object、Function關(guān)系介紹  回復(fù)  更多評(píng)論   

    支持 博主
    2013-10-21 15:31 | 零柒鎖業(yè)

    # re: Js中Prototype、__proto__、Constructor、Object、Function關(guān)系介紹[未登錄](méi)  回復(fù)  更多評(píng)論   

    啥都不說(shuō)了 解惑了 十分感謝
    2015-04-27 18:47 | dd

    # re: Js中Prototype、__proto__、Constructor、Object、Function關(guān)系介紹[未登錄](méi)  回復(fù)  更多評(píng)論   

    不過(guò)我測(cè)試的是
    Object.constructor
    返回:function Function() { [native code] }
    Object.prototype.constructor
    返回:function Object() { [native code] }
    也不是同一個(gè)啊 圖中畫(huà)的指向是同一個(gè) 這是怎么回事兒?望解惑
    2015-04-27 18:54 | dd

    # re: Js中Prototype、__proto__、Constructor、Object、Function關(guān)系介紹[未登錄](méi)  回復(fù)  更多評(píng)論   

    Object.constructor
    返回:function Function() { [native code] }
    Object.prototype.constructor
    返回:function Object() { [native code] }

    都回指向內(nèi)部__proto__
    2015-05-07 16:21 | bb

    # re: Js中Prototype、__proto__、Constructor、Object、Function關(guān)系介紹  回復(fù)  更多評(píng)論   

    @dd
    Object.constructor,其實(shí)是取取值于Function.prototype中的Constructor,即為Function.而Object.prototype.Constructor卻是指向構(gòu)造該對(duì)象的函數(shù)即,Object函數(shù).
    2015-06-01 18:30 | 天厴

    # re: Js中Prototype、__proto__、Constructor、Object、Function關(guān)系介紹  回復(fù)  更多評(píng)論   

    (i.e. Animal.prototype.constructor===Animal)
    這句的前提是 Animal沒(méi)有原型對(duì)象, 否則應(yīng)該全等于原型對(duì)象所處的類(lèi)型本身。
    2015-07-08 16:53 | 店小二

    # re: Js中Prototype、__proto__、Constructor、Object、Function關(guān)系介紹[未登錄](méi)  回復(fù)  更多評(píng)論   

    Function.prototype沒(méi)有prototype屬性,作為函數(shù)對(duì)象有什么用?為什么不直接令他為普通對(duì)象
    2015-08-06 23:32 | wu

    # re: Js中Prototype、__proto__、Constructor、Object、Function關(guān)系介紹  回復(fù)  更多評(píng)論   

    為啥object其實(shí)是一個(gè)函數(shù)對(duì)象。js里萬(wàn)物皆對(duì)象。。但object的原型屬性為啥要指向function.prototype.不是object本身也有個(gè)prototype對(duì)象嗎
    2015-09-26 10:11 | 張漢卿

    # re: Js中Prototype、__proto__、Constructor、Object、Function關(guān)系介紹  回復(fù)  更多評(píng)論   

    博主好人
    2015-11-19 10:39 | 秋天的早晨

    # re: Js中Prototype、__proto__、Constructor、Object、Function關(guān)系介紹  回復(fù)  更多評(píng)論   

    @dd
    兩個(gè)還是有區(qū)別的
    2015-12-30 15:30 | kingx

    # re: Js中Prototype、__proto__、Constructor、Object、Function關(guān)系介紹[未登錄](méi)  回復(fù)  更多評(píng)論   

    new Animal指向好像錯(cuò)了,貌似是直接指向Animal.prototype
    2016-01-02 17:43 | lei

    # re: Js中Prototype、__proto__、Constructor、Object、Function關(guān)系介紹  回復(fù)  更多評(píng)論   

    @lei
    確實(shí)
    2016-01-05 16:53 | co

    # re: Js中Prototype、__proto__、Constructor、Object、Function關(guān)系介紹  回復(fù)  更多評(píng)論   

    博主寫(xiě)的這篇文章真牛逼,解惑!
    2016-05-18 10:22 | 馮琨皓

    只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: www.亚洲一区| 无码国产精品一区二区免费式影视 | 久久久久久久久久国产精品免费| 一级毛片完整版免费播放一区| 色费女人18女人毛片免费视频| 国产成人高清亚洲一区久久| 99亚洲精品卡2卡三卡4卡2卡| 国产精品亚洲а∨无码播放不卡| 国产综合激情在线亚洲第一页 | 久久夜色精品国产亚洲AV动态图| 亚洲AV无码1区2区久久| 亚洲Av熟妇高潮30p| 亚洲欧洲精品国产区| 亚洲H在线播放在线观看H| 中文字幕亚洲情99在线| 亚洲精品日韩一区二区小说| 精品国产_亚洲人成在线| 四虎影视在线看免费观看| 成全视成人免费观看在线看| 七色永久性tv网站免费看| 免费观看黄色的网站| 91在线品视觉盛宴免费| 免费国产精品视频| JLZZJLZZ亚洲乱熟无码| 亚洲AV综合色区无码另类小说| 91亚洲一区二区在线观看不卡| 亚洲中字慕日产2020| 激情小说亚洲图片| 丁香花在线观看免费观看图片| 特级无码毛片免费视频尤物 | 国产精品免费一区二区三区| 日本免费一区二区久久人人澡| 亚洲黄色免费网址| 国产乱子伦精品免费无码专区| 亚洲人精品午夜射精日韩| 亚洲精品国产成人| 牛牛在线精品免费视频观看| 日韩精品免费视频| 免费无码黄网站在线观看| 亚洲日韩精品一区二区三区无码| 亚洲国产综合自在线另类|