继承
实例对象可以调用其构造函数原型中的方法以及其构造函数父函数原型中的方法…
var arr = [1,2,3];
arr -> Array.prototype -> Object.prototype
Animal Dog
Dog继承Animal
dog -> Dog.prototype -> Animale.prototype
- 借用构造函数
function Aniaml(name,age){
this.name = name;
this.age = age;
}
Animal.prototype = {
constructor :Animal,
sayName:function(){
console.log('my name is ',this.name);
},
sayAge:function(){
console.log('my age is ',this.age);
}
}
function Dog(name,age,gender){
//借用构造函数
Animal.call(this,name,age);
/*
this.name = name;
this.age = age;
*/
this.gender = gender;
}
- 原型链继承
Dog.prototype = new Animal();
Dog.prototype.constructor = Dog;
Dog.prototype.sayGender = function(){
}