//1.工厂模式
function createAnimal(name,color){
var o = new Object();
o.name = name;
o.color = color;
o.show = function(){
document.write("<br/>" +this.name + this.color)
}
return o;
}
var cat1 = createAnimal("tom","white");
console.log(cat1);
cat1.show();
//2.构造函数模式
function createAnimal2(name,color){
this.name = name;
this.color = color;
this.show = function(){
document.write("<br/>" +this.name + this.color)
}
}
var cat2 = new createAnimal2("tom","red");
console.log(cat2);
cat2.show();
//3.原型链模式
function createAnimal3(name,color){
createAnimal3.prototype.name = name;
createAnimal3.prototype.color = color;
createAnimal3.prototype.show = function(){
document.write("<br/>" +this.name + this.color)
}
}
var cat3 = new createAnimal3("lili","black");
console.log(cat3);
cat3.show();
//4.构造+原型模式
function createAnimal4(name,color){
this.name = name;
this.color = color;
}
createAnimal4.prototype.show = function(){
document.write("<br/>" + this.name +this.color)
}
var cat4 = new createAnimal4("lulu","blue");
console.log(cat4);
cat4.show();