javascript继承最完美的方法
假如现在有两个构造函数Person和Animal,现在要让Animal去继承Person中的属性和方法,最完美的方法如下:
function Person(name,age,sex){
this.name = name;
this.age = age;
this.sex = sex;
}
Person.prototype = {
message:function(){
alert(this.name+','+this.age+','+this.sex);
}
}
function Animal(legs){
Person.apply(this.arguments);
this.legs = legs;
}
Animal.prototype = new Person();
Animal.prototype.constructor = Animal;
到此结束,animal已经继承了person的属性和方法,这是效率最高的一种继承方式了
浙公网安备 33010602011771号