1.用构造函数封装对象的属性和方法。
1 function Cat(name,color){ 2 this.name=name; 3 4 this.color=color; 5 6 } 7 8 var cat1 = new Cat("大毛","黄色"); 9 10 var cat2 = new Cat("二毛","黑色");
2.用原型继承模式
function Cat(name,color){ this.name = name; this.color = color; } Cat.prototype.type = "猫科动物"; Cat.prototype.eat = function() {alert("吃老鼠")}; var cat1 = new Cat("大毛","黄色"); var cat2 = new Cat("二毛","黑色"); alert(cat1.type); // 猫科动物 cat1.eat(); // 吃老鼠