-
Notifications
You must be signed in to change notification settings - Fork 891
Open
Description
const person = { name: "yideng" };
function sayHi(age) {
return `${this.name} is ${age}`;
}
console.log(sayHi.call(person, 21));
console.log(sayHi.bind(person, 21));
每日一题会在下午四点在交流群集中讨论,五点小程序中更新答案
欢迎大家在下方发表自己的优质见解
二维码加载失败可点击 小程序二维码
Activity
Genzhen commentedon Jun 22, 2020
答案
yideng is 5 ƒ sayHi(age) {return
${this.name} is ${age}
;}解析
使用两者,我们可以传递我们想要
this
关键字引用的对象。 但是,.call
方法会立即执行!.bind
方法会返回函数的拷贝值,但带有绑定的上下文! 它不会立即执行。123456zzz commentedon Jul 14, 2020
yideng is 21 错了
Genzhen commentedon Jul 15, 2020
已更正,感谢指出
a123456789B commentedon Sep 4, 2020
VM117:6 yideng is 5
VM117:7 ƒ sayHi(age) {
return
${this.name} is ${age}
;}
bind 的返回是一个函数,要在调用时传参
console.log(sayHi.bind(person, 5)()); 这样输入才会出现和call 出现相同的结果
JackingHuang commentedon Jan 4, 2022
应该是 yideng is 21 ƒ sayHi(age) {return ${this.name} is ${age};} ;yideng is 5 这个 5从什么地方来的???