call 是 JavaScript 中用于显式指定函数执行时 this 指向的方法,核心用法为 func.call(thisArg, arg1, arg2, ...),立即执行并绑定 this;常用于借用数组方法处理类数组、构造函数继承及 Object.prototype.toString.call() 安全判型。

call 是 JavaScript 中用于显式指定函数执行时 this 指向的方法,常用来“借用”其他对象的方法来操作当前数据。
核心用法:改变 this 指向并传参
func.call(thisArg, arg1, arg2, ...) 会立即执行 func,并将 this 绑定为 thisArg,后续参数依次传入。
- 第一个参数是函数内部
this所指向的对象(可以是 null/undefined,此时非严格模式下指向全局对象,严格模式下为 undefined) - 从第二个参数开始,是传递给目标函数的实际参数
常见借用场景:数组方法处理类数组对象
比如 arguments 或 DOM 节点列表不是真正的数组,但想用 Array.prototype.slice 转成数组:
function foo() {
// arguments 是类数组,没有 slice 方法
const arr = Array.prototype.slice.call(arguments);
console.log(arr); // [1, 2, 3]
}
foo(1, 2, 3);
这里借用了 Array.prototype.slice,把 arguments 当作 this 传入,就能正确截取。
立即学习“Java免费学习笔记(深入)”;
借用构造函数实现继承
在传统构造函数继承中,子类常通过 Parent.call(this, ...args) 初始化父类属性:
function Animal(name) {
this.name = name;
}
function Dog(name, breed) {
Animal.call(this, name); // 借用 Animal 构造函数,让 this 指向新 Dog 实例
this.breed = breed;
}
const dog = new Dog('旺财', '金毛');
这样 dog 就拥有了 name 和 breed 属性,避免了原型链上共享引用类型的问题。
借用内置方法简化操作
例如用 Object.prototype.toString.call() 安全判断数据类型:
console.log(Object.prototype.toString.call([])); // [object Array] console.log(Object.prototype.toString.call(null)); // [object Null] console.log(Object.prototype.toString.call(123)); // [object Number]
比直接调用 toString() 更可靠,因为很多对象重写了该方法,而 Object.prototype.toString 是最原始、可信赖的类型检测方式。


















