call 和 apply
call和apply都可以理解为是函数自身的方法 第一个参数代表了 接下来this所代表的对象
call和apply的区别
使用call方法来传参 一个一个的传参 使用apply方法来传参 需要传一个数组 数组里面的第一个就对应了f函数里面的 第一个参数,数组里面的第二个就对应了f函数里面的 第二个参数
组合继承
- 也叫伪经典继承
- 将原型链继承和构造函数继承组合在一块
- 原型链实现对原型属性和方法的继承
- 借用构造函数实现对实例属性的继承
function Car(){
this.lunzi = 4;
}
Car.prototype.run = function (){
document.write(`
${this.name} 我有${this.lunzi}个轮子 会跑赛道
`);
}
function Bc(name){
Car.call(this)
this.name = name;
}
Bc.prototype = new Car();
Bc.prototype.constructor = Bc;
let bc1 = new Bc('奔驰')
console.log(bc1);
bc1.run();
拷贝继承
- 把父对象的所有属性和方法,拷贝进子对象
- 将父对象的prototype对象中的属性,
- 一一拷贝给Child对象的prototype对象
<script>
function fz(zilei, fulei) {
for (let key in fulei.prototype) {
zilei.prototype[key] = fulei.prototype[key];
}
}
function Father(name, yuwen, shuoxue) {
this.name = name;
this.yuwen = yuwen;
this.shuoxue = shuoxue;
this.roo = function () {
document.write(
`
姓名: ${this.name}<br>
语文: ${this.yuwen}<br>
数学: ${this.shuoxue}<br>
年龄: ${this.age}<br>
`
)
}
}
function Sun(age){
Father.call(this,'张三',99,90)
this.age = age;
}
fz(Sun,Father);
let father = new Father();
let sun = new Sun('巅峰');
console.log(sun);
sun.roo();
</script>