每日一题(十四)const person = {name:'leo'}; function say(age){ return `${this.name} is ${age}`; } conso

题目描述:写出输出结果,并解释原因

const person = {name:'leo'};
function say(age){
    return `${this.name} is ${age}`;
}
console.log(say.call(person,5));
console.log(say.bind(person,5))

答案:

leo is 5

ƒ say(age){
    return `${this.name} is ${age}`;
}

解析:

  • 使用两者,可以传递我们想要`this`关键字引用的对象。 但是,`.call`方法会立即执行!
  • .bind方法会返回函数的拷贝值,但带有绑定的上下文! 它不会立即执行。

你可能感兴趣的:(每日一题,每日一题,前端面试题,JS基础,call,bind)