原型继承
//无法实现多重继承,创建了一个子类的实例
//如果对子类实例的属性进行了修改,那么创建其他子类的时候都会受到影响
function superfunc(){
this.name = "baiying";
this.age = 18;
this.getName = function(){
alert(this.age +"岁的"+this.name);
}
}
function sub(){
this.hobby = 'eat';
}
sub.prototype = new superfunc();
var huanglizhen = new sub();
console.log(huanglizhen.hobby); //eat
huanglizhen.getName();//alert "18岁的baiying"
借用构造函数:可传参
function superfunc1(){
this.name = "baiying";
this.age = 18;
this.getName = function(){
alert(this.age +"岁的"+this.name);
}
}
function sub1(){
this.hobby = 'eat';
superfunc1.call(this);
}
var huanglizhen = new sub();
console.log(huanglizhen.hobby); //eat
huanglizhen.getName();//alert "18岁的baiying"