<code>function person(name,age){
this.name = name;
this.age = age;
}
person.prototype.say = function(){
console.log(this.name+":"+this.age);
}
function superman(name,age){
person.call(this,name,age);
}
superman.prototype = new person();
var s = new superman('superman',29);
</code>在书上看到这种继承方式,说很完美,可是我并不觉得啊,因为他的superman.prototype = new person();这句,会将父类的实例属性添加到子类的原型上啊,虽然person.call(this,name,age);已经拿到了父类的实例属性,但是感觉这样污染了子类的原型啊,怎么破?
<code>function object(obj){
function F(){}
F.prototype = obj;
return new F();
}
function inheritProtoType(SuperType,SubType){
var prototype = object(SuperType.prototype);
prototype.constructor = SubType;
SubType.prototype = prototype;
}
function SuperType(){
this.name = 'yuhualingfeng';
this.friends = ['David','Bob','Lucy'];
}
SuperType.prototype.saySuperName = function(){
console.log(this.name);
};
function SubType(){
SuperType.call(this);
this.age = 30;
}
inheritProtoType(SuperType,SubType);
SubType.prototype.saySubName = function(){
console.log(this.name);
};
var subType = new SubType();
</code>
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号