javascript - 關於js的繼承方式,求解!

來源:互聯網
上載者:User
function person(name,age){    this.name = name;    this.age = age;}person.prototype.say = function(){    console.log(this.name+":"+this.age);}function superman(name,age){    person.call(this,name,age);}superman.prototype = new person();var s = new superman('superman',29);

在書上看到這種繼承方式,說很完美,可是我並不覺得啊,因為他的superman.prototype = new person();這句,會將父類的執行個體屬性添加到子類的原型上啊,雖然person.call(this,name,age);已經拿到了父類的執行個體屬性,但是感覺這樣汙染了子類的原型啊,怎麼破?

好了,問題解決了,使用寄生組合式繼承可以解決這個問題

function object(obj){ function F(){} F.prototype = obj; return new F();}function inheritProtoType(SuperType,SubType){     var prototype = object(SuperType.prototype);     prototype.constructor = SubType;     SubType.prototype = prototype;}function SuperType(){    this.name = 'yuhualingfeng';    this.friends = ['David','Bob','Lucy'];}SuperType.prototype.saySuperName = function(){    console.log(this.name);};function SubType(){    SuperType.call(this);    this.age = 30;}inheritProtoType(SuperType,SubType);SubType.prototype.saySubName = function(){    console.log(this.name);};var subType = new SubType();

回複內容:

function person(name,age){    this.name = name;    this.age = age;}person.prototype.say = function(){    console.log(this.name+":"+this.age);}function superman(name,age){    person.call(this,name,age);}superman.prototype = new person();var s = new superman('superman',29);

在書上看到這種繼承方式,說很完美,可是我並不覺得啊,因為他的superman.prototype = new person();這句,會將父類的執行個體屬性添加到子類的原型上啊,雖然person.call(this,name,age);已經拿到了父類的執行個體屬性,但是感覺這樣汙染了子類的原型啊,怎麼破?

好了,問題解決了,使用寄生組合式繼承可以解決這個問題

function object(obj){ function F(){} F.prototype = obj; return new F();}function inheritProtoType(SuperType,SubType){     var prototype = object(SuperType.prototype);     prototype.constructor = SubType;     SubType.prototype = prototype;}function SuperType(){    this.name = 'yuhualingfeng';    this.friends = ['David','Bob','Lucy'];}SuperType.prototype.saySuperName = function(){    console.log(this.name);};function SubType(){    SuperType.call(this);    this.age = 30;}inheritProtoType(SuperType,SubType);SubType.prototype.saySubName = function(){    console.log(this.name);};var subType = new SubType();

Object.create(Person.prototype);

這個可以有效解決,不過要注意相容性

function create(obj) {    if (Object.create) {        return Object.create(obj);    }    function f() {};    f.prototype = obj;    return new f();}

繼承 person.prototype

繼承而不是汙染

  • 聯繫我們

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