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)
原型对象:Javascript的继承是基于原型的继承<br /> 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 = 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)
1.封装<br /> 2.继承<br /> 3.多态 (自带多态)
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,{
constructor:{
value:Teacher
}
})
Teacher.prototype.sayAge = function () {
console.log(this.age)
}
var t = new Teacher("zhang", 18, "js");
console.log(t.constructor)
t.sayName();