1.属性继承:call
<script>
// 定义两个类,Person Teacher
function Person(name,age){
this.name = name;
this.age = age;
}
function Teacher(name,age,skill){
var self = this;
Person.call(self,name,age);//直接调用Person继承属性,call改变Person内部this指向
this.skill = skill;
}
var t = new Teacher("zhang",18,"js");
console.log(t)
//输出Teacher{name:"zhang",age:18,skill:"js"}
</script>
2.方法继承
Teacher.prototype = Person.prototype;
重置构造属性:Teacher.prototype.constructor = Teacher;
<script>
/* Person Teacher */
/*
原型对象:Javascript的继承是基于原型的继承
call能够实现属性的继承
*/
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)
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(t.constructor)
</script>