javascript內部原理篇[__proto__和prototype]

來源:互聯網
上載者:User

javascript的__proto__(注意前後都是兩條底線)和prototype都可以訪問對象的原型對象,那麼他們之間有什麼不同嗎?

是的,__proto__是只能用 對象. 來訪問,而prototype則只能是 類. 來訪問

下面簡單舉例:

 /**     * 利用原型模式實現簡單繼承     */    function per(){        this.getName = function(str){            alert(str);        }    }    per.prototype.getAge = function(age){        alert(age);    }    var a = {};    a.__proto__ = per.prototype;    //a.getAge(2);   // a.getName("a");    /**     * 簡單方式實現繼承     */    var b = {};    b.__proto__ = new per();    b.__proto__.constructor = b;    b.getAge("1");    b.getName("a");    /**     * 串聯繼承     */    function A(){        this.getA = "a";    }    function B(){        this.getB = "b";    }    function K(){}    B.prototype = new A();    B.prototype.constructor = B;    K.prototype = new B();    K.prototype.constructor = K;    var test = new K();    alert(test.getA);    alert(test.getB);

通過以上例子大家可以簡單看到其中的區別

歡迎大家在評論中討論此問題,我會及時回複

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.