标签:
function Father(lastname,sex){ this.lastname=lastname; //姓 this.sex=sex;//性别 } //父类原型属性 Father.prototype.xo=‘xxx‘; //子类 function Son(firstname){ this.firstname=firstname;//名 } Son.prototype=new Father(‘杨‘); //原型继承,son的原型已经指向了father对象的实例 var son = new Son(‘清‘); alert(son.firstname);//清 alert(son.lastname);//杨 继承的属性 alert(son.xo);//xxx 继承的原型属性 alert(son.sex);//undefined//继承时未传值
缺点:继承后还必须给父类传参, new Father(‘杨‘), 不能把参数直接传入子类中
//父类 function Father(lastname,sex){ this.lastname=lastname; //姓 this.sex=sex;//性别 } //父类原型属性 Father.prototype.xo=‘xxx‘; //子类 function Son(lastname,firstname){ Father.call(this,lastname);//将Father构造函数绑定到Son this.firstname=firstname;//名 } var son = new Son(‘杨‘,‘清‘); alert(son.firstname);//清 alert(son.lastname);//杨 继承的属性 alert(son.xo);//undefined 未能继承到原型属性 alert(son.sex);//undefined//不需要继承属性
缺点:未能继承到原型属性
function Father(lastname,sex){ this.lastname=lastname; //姓 this.sex=sex;//性别 } //父类原型属性 Father.prototype.xo=‘xxx‘; //子类 function Son(lastname,firstname){ Father.call(this,lastname);//借用构造函数继承 this.firstname=firstname;//名 } Son.prototype=new Father(); //原型继承,不用传参 只为继承原型属性 var son = new Son(‘杨‘,‘清‘); alert(son.firstname);//清 alert(son.lastname);//杨 继承的属性 alert(son.xo);//xxx 继承到原型属性 alert(son.sex);//undefined//不需要继承属性
标签:
原文地址:http://www.cnblogs.com/yangjingqi/p/4339592.html