(三十三)設計模式之混合模式


/** * 混合模式 = 原型模式 + 構造函數模式 */ 
function Animal(name, color){ 
    this.name = name; 
    this.color = color; 
    console.log( this.name + this.color) 
} 
Animal.prototype.getInfo = function(){ 
    console.log('名稱:'+ this.name); 
} 
function largeCat(name, color){ 
    Animal.call(null, name, color); 
    this.color = color; 
} 
largeCat.prototype = create(Animal.prototype); 
function create (parentObj){ 
    function F(){} 
    F.prototype = parentObj; 
    return new F(); 
}; 
largeCat.prototype.getColor = function(){ return this.color; } 
var cat = new largeCat("Persian", "白色"); console.log( cat )

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM