一、生成对象的原始模式
var Cat={ name:'', color:'' } var cat1={}; cat1.name="Cat_one"; cat1.color="yellow";
二、原始模式改进
function Cat(name,color){ return { name:name, color:color } } var cat1 = Cat("大毛","黄色");
三、构造函数模式
function Cat(name,color){ this.name=name; this.color=color; } var cat1 = new Cat("大毛","黄色");
四、Prototype模式
function Cat(name,color){ this.name = name; this.color = color; } Cat.prototype.type = "猫科动物"; Cat.prototype.eat = function(){alert("吃老鼠")}; //or /** Cat.prototype={ type:'猫科动物', eat:function(){alert('吃老鼠')} }; **/
五、常用方法
isPrototypeOf() //这个方法用来判断,某个proptotype对象和某个实例之间的关系。 hasOwnProperty() //每个实例对象都有一个hasOwnProperty()方法,用来判断某一个属性到底是本地属性,还是继承自prototype对象的属性。