Persom Teacher
call能够实现属性的继承
// Persom Teacher
// call能够实现属性的继承
function Person(name,age){
this.name = name;
this.age = age;
}
function Teacher(name,age,skill){
var self = this;
Person.call(self,name,age)
this.skill = skill;
}
var t = new Teacher("zhang",18,"js");
console.log(t);
demo
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 = Person.prototype;
Teacher.prototype.sayAge = function(){
console.log(this.age);
}
Teacher.prototype.constructor = Teacher;
var t = new Teacher("zhang",18,"js");
var p = new Person("lisi",13);
console.log(t.constructor);
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);
this.skill = skill;
}
// 方法的继承
Teacher.prototype = Object.create(Person.prototype,{
constructo:{
value:Teacher
}
})
Teacher.prototype.sayAge = function(){
console.log(this.age);
}
var t = new Teacher("zhang",18,"js");
console.log(t.constructor);
拓展
// function Person(name,age){
// this.name = name;
// this.age = age;
// }
// Person.prototype = {
// constructor:Person,
// sayName(){
// console.log(this.name);
// }
// }
class Person{
constructor(name,age){
this.name = name;
thisage = age;
}
sayName(){
console.log(this.name)
}
}
var p = new Person("heng",19)
console.log(p);