原型鏈-P14-2019-12-06

測(cè)試代碼.png

原型鏈.png
//代碼如下
   <script>
        //原形本質(zhì)是對(duì)象所以我們可以動(dòng)態(tài)的添加屬性和方法
        function Student(age, name, sex) {
            this.name = name;
            this.age = age;
            this.sex = sex;

            // this.sayHi = function() {
            //     console.log("111");
            // }
        }
        // console.dir(Student.prototype);
        Student.prototype.sayHi = function() {
            console.log("大家好,我是" + this.name);
        }

        var st1 = new Student(18, "張三", "男")
        var st2 = new Student(19, "韓梅梅", "女")
            // st2.sayHi();
            // st2.say(); 錯(cuò)誤 頂層為null
        var o = st1.__proto__;
        console.dir(o);
    </script>
在上面圖中根據(jù)原型三角關(guān)系.png

可知現(xiàn)在只是查找到了構(gòu)造函數(shù)擁有的原形而已所以可以找到

//再如下代碼
    <script>
        //原形本質(zhì)是對(duì)象所以我們可以動(dòng)態(tài)的添加屬性和方法
        function Student(age, name, sex) {
            this.name = name;
            this.age = age;
            this.sex = sex;

            // this.sayHi = function() {
            //     console.log("111");
            // }
        }
        // console.dir(Student.prototype);
        Student.prototype.sayHi = function() {
            console.log("大家好,我是" + this.name);
        }

        var st1 = new Student(18, "張三", "男")
        var st2 = new Student(19, "韓梅梅", "女")
            // st2.sayHi();
            // st2.say(); 錯(cuò)誤 頂層為null
        var o = st1.__proto__;
        console.dir(o);
        var o1 = st1.__proto__;
        console.dir(o1.__proto__);
    </script>
var o1 = st1.__proto__;.png
//繼續(xù)
   <script>
        //原形本質(zhì)是對(duì)象所以我們可以動(dòng)態(tài)的添加屬性和方法
        function Student(age, name, sex) {
            this.name = name;
            this.age = age;
            this.sex = sex;

            // this.sayHi = function() {
            //     console.log("111");
            // }
        }
        // console.dir(Student.prototype);
        Student.prototype.sayHi = function() {
            console.log("大家好,我是" + this.name);
        }

        var st1 = new Student(18, "張三", "男")
        var st2 = new Student(19, "韓梅梅", "女")
            // st2.sayHi();
            // st2.say(); 錯(cuò)誤 頂層為null
        var o = st1.__proto__;
        console.dir(o);
        //原型中的proto是什么類型
        var o1 = o.__proto__;
        console.dir(o1);

        var o2 = o1;
        console.dir(o2.__proto__);
    </script>
image.png

由此可以引出成員查找規(guī)則
會(huì)先再這個(gè)對(duì)象中查找,如果沒(méi)有會(huì)去原型如再?zèng)]有會(huì)去原型鏈查如果有沒(méi)頂層null直接返回


image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容