// 示例代码:使用 call 方法
function greet() {
console.log(`Hello, I am ${this.name}`);
}
const person1 = { name: 'Alice' };
const person2 = { name: 'Bob' };
// 使用 call 方法调用 greet 函数,并将 this 绑定到 person1 对象
greet.call(person1); // 输出: Hello, I am Alice
// 使用 call 方法调用 greet 函数,并将 this 绑定到 person2 对象
greet.call(person2); // 输出: Hello, I am Bob
call 方法用于调用一个函数,并指定该函数内部的 this 值。它还可以传递参数给该函数。greet 函数通过 call 方法被调用,this 被分别绑定到了 person1 和 person2 对象上,因此输出的结果会根据 this.name 的不同而变化。上一篇:js call apply
下一篇:js call
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站