function fun(){
console.log(this.name);
}
let cat = {
name:"喵喵"
}
//call可以调用函数,可以改变函数中的this指向
fun.call(cat)
2.
let dog = {
name:'旺财',
sayName(){
console.log("我是"+this.name);
},
eat(food){
console.log("我喜欢吃"+food);
}
}
let cat = {
name:"喵喵"
}
dog.eat("骨头")
3.如果这样写 dog.eat.call(cat,“鱼”)
dog.eat.call(cat,"鱼")
4.call、apply、bind的区别
dog.eat.call(cat,"鱼","肉") //输出:我喜欢吃鱼肉
dog.eat.apply(cat,["鱼","肉"])//输出:我喜欢吃鱼肉
dog.eat.bind(cat,"鱼","肉")//无输出
let fun = dog.eat.bind(cat,"鱼","肉")
fun()//与dog.eat.call(cat,"鱼","肉")一模一样,输出:我喜欢吃鱼肉