1.call的使用
(1)可以调用函数
//创建一个构造函数
function Star(name, age) {
this.name = name
this.age = age
}
//用call方法调用构造函数
Star.call('李四', 18)
(2)改变this指向
//创建一个父构造函数
function Father(name, age) {
this.name = name
this.age = age
}
//给父构造函数原型对象prototype添加方法
Father.prototype.money = function() {
console.log('1万元')
创建一个构造函数
function Son(name, age, score) {
this.score = score
//利用call方法将this指向父构造函数,然后调用父构造函数里面的属性,实现继承
Father.call(this, name, age)
}
let son = new Son('张三', 19, 100)
console.log(son)