继承(经典继承/混合继承/组合式继承):
一、当B构造函数继承A构造函数的时候,在B构造函数中 借用父构造函数继承父构造函数的属性 代码:A.call(this,参数)
```
function A(name,age){
this.name = name.slice(0,1);
this.age = age;
}
function B(name,age,gender){
var gender = gender;
// 借用父构造函数 继承父构造函数的属性
A.call(this,name,age);
}
var b = new B('张三',20,'男');
console.log(b);
```
二、通过 B构造函数的.prototype.方法名=A构造函数.prototype.方法名 来继承原型上的方法
```
// 让所有 用A构造函数创建出来的对象 都共用原型上的方法 (一个say方) 原型:
A.prototype.say =function(){
console.log(this.name)
}
var a1 = new A('s',1)
var a2 = new A('s2',1)
console.log(a1.say == a2.say);//true
B.prototype.say = A.prototype.say;
var b = new B('张三',20,'男');
b.say();
```
原型:prototype
在构造函数的原型上,添加一个方法,通过这个构造函数创建出来的对象,都共用原型上的方法 (一个say方)