子类继承父类的方法(单重继承)

function Person(){
this.name="刮大白";
this.sayName=function(){
console.log(this.name);
}
}

Person.prototype.constructor=Person;
Person.prototype.sex="女生";
Person.prototype.sayHello=function(){
console.log("Hi");
};

//子类继承父类的全部属性或方法
function smallPerson(){
Person.call(this);//子类继承父类的实例方法1
this.base=Person;//子类继承父类的实例方法2
this.base();
}

smallPerson.prototype=new Person();//子类继承父类的原型方法1
smallPerson.prototype=Object.create(Person.prototype);//子类继承父类的原型方法2

 

//子类继承父类的某一个方法
smallPerson.prototype.sayHello=function(){
Person.prototype.sayHello.call(this);
};

var o=new smallPerson();
console.log(o.name);//刮大白
o.sayHello();//Hi
o.sayName();//刮大白

posted on 2017-10-12 17:48  龟和兔子赛跑  阅读(185)  评论(0)    收藏  举报

导航