1.
<script> //原型对象:javascript 的继承是基于原型的继承 function Person(name,age){ this.name=name; this.age=age; } Person.prototype.sayName=function(){ console.log(this.name); } function Teacher(name,age,skill){ Person.call(this,name,age); //call能够实现属性的继承 this.skill=skill; } Teacher.prototype.sayAge=function(){ console.log(this.age); } Teacher.prototype=Person.prototype; Teacher.prototype.constructor=Teacher; var t=new Teacher("zhang",18,"js"); var p=new Person("lisi",13) console.log(p.constructor);</script>
2.
<script>
/*面向对象的三种方法
1.封装
2.继承
3.多态(自带多态)
*/
//es5中实现继承的方式
function Person(name,age){
this.name=name;
this.age=age;
}
Person.prototype.sayName=function(){
console.log(this.name);
}
function Teacher(name,age,skill){
Person.call(this,name,age); //call能够实现属性的继承
this.skill=skill;
}
Teacher.prototype.sayAge=function(){
console.log(this.age);
}
Teacher.prototype=Object.create(Person.prototype,{
constructor:{
value:Teacher
}
})
var t=new Teacher("zhang",18,"js");
console.log(t);
t.sayName(); //检查是否继承
</script>