继承简介
继承是面向对象编程中的一个重要概念,通过继承可以使子类的实例使用在父类中定义的属性和方法.
实现继承的几种方式
第一种方式原型链继承
let Func = function() {
this.name = '我是Func';
}
let subFunc = new Func();
console.log(subFunc.name);
第二种方式call, apply继承
let FuncCall = function () {
this.name = '我是FuncCall';
this.getName = function(){
return this.name;
}
};
let subFuncCall = function() {
FuncCall.call(this);
// FuncCall.apply(this);
this.age = 18;
}
let aFunc = new subFuncCall();
console.log(aFunc.name);
console.log(aFunc.age);
console.log(aFunc.getName());
// 单独继承某个方法
Math.max.apply(Array, [1,2,3,4]); // 4
Math.max.call(Array, ...[1,2,3,4]); // 4
call和apply的参数区别
第一个参数的值赋值给类(即方法)中出现的this
call的第二个参数开始依次赋值给类(即方法)所接受的参数;bind的第二个参数是一个数组类型