阅读本教程后,我想我对如何使用 call 和 apply 有了一些了解。但是,我仍然有点困惑。
是否存在 usingfunction.call(this, arguments)与 using 不同的情况this.function(arguments)?
如果没有,那我们为什么需要这个call函数?
const person = {
name: "Bob",
greet: function() { console.log("Hello " + this.name) }
};
const thank = function() {
console.log("Thanks " + this.name);
}
Run Code Online (Sandbox Code Playgroud)
person.greet()与 相同person.greet.call(person),但第一个更简洁,因此这就是此变体存在的原因。
call当函数不是对象的成员时,函数很有用。你不能打电话person.thank(),你必须打电话thank.call(person)。
| 归档时间: |
|
| 查看次数: |
147 次 |
| 最近记录: |