此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。
Function.prototype.call()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
Function 实例的call() 方法会以给定的this 值和逐个提供的参数调用该函数。
In this article
尝试一下
function Product(name, price) { this.name = name; this.price = price;}function Food(name, price) { Product.call(this, name, price); this.category = "food";}console.log(new Food("cheese", 5).name);// Expected output: "cheese"语法
call(thisArg)call(thisArg, arg1)call(thisArg, arg1, arg2)call(thisArg, arg1, arg2, /* …, */ argN)参数
thisArg在调用
func时要使用的this值。如果函数不在严格模式下,null和undefined将被替换为全局对象,并且原始值将被转换为对象。arg1, …, argN可选函数的参数。
返回值
使用指定的this 值和参数调用函数后的结果。
描述
备注:这个函数几乎与apply() 相同,只是函数的参数以列表的形式逐个传递给call(),而在apply() 中它们被组合在一个对象中,通常是一个数组——例如,func.call(this, "eat", "bananas") 与func.apply(this, ["eat", "bananas"])。
通常,在调用函数时,函数内部的this 值是访问该函数的对象。使用call(),你可以在调用现有函数时将任意值分配给this,而无需首先将函数附加到对象上作为属性。这样可以将一个对象的方法用作通用的实用函数。
警告:不要使用call() 来链式调用构造函数(例如,实现继承)。这会将构造函数作为普通函数调用,这意味着new.target 的值为undefined,而类会抛出错误,因为它们不能在没有new 的情况下被调用。请改用Reflect.construct() 或extends。
示例
>使用 call() 调用函数并指定 this 值
在下面的示例中,当我们调用greet 时,this 的值将绑定到对象obj,即使greet 不是obj 的方法。
function greet() { console.log(this.animal, "的睡眠时间一般在", this.sleepDuration, "之间");}const obj = { animal: "猫", sleepDuration: "12 到 16 小时",};greet.call(obj); // 猫 的睡眠时间一般在 12 到 16 小时 之间使用 call() 在不指定第一个参数的情况下调用函数
如果省略第一个thisArg 参数,则默认为undefined。在非严格模式下,this 值将被替换为globalThis(类似于全局对象)。
globalThis.globProp = "Wisen";function display() { console.log(`globProp 的值是 ${this.globProp}`);}display.call(); // 输出“globProp 的值是 Wisen”在严格模式下,this 的值不会被替换,因此它保持为undefined。
"use strict";globalThis.globProp = "Wisen";function display() { console.log(`globProp 的值时 ${this.globProp}`);}display.call(); // 抛出 TypeError: Cannot read the property of 'globProp' of undefined将方法转换为实用函数
call() 几乎等同于普通函数调用,只是将this 作为普通参数传入,而不是作为函数所在的对象值。这类似于通用的实用函数的工作方式:你可以使用map(array, callback) 来代替array.map(callback),这样可以避免对Array.prototype 进行修改,还可以将map 用于不是数组的类数组对象(例如arguments)。
以Array.prototype.slice() 为例,你想要将类数组对象转换为真正的数组。你可以创建一个类似这样的快捷方式:
const slice = Array.prototype.slice;// ...slice.call(arguments);请注意,你不能将slice.call 保存并将其作为普通函数调用,因为call() 方法也会读取它的this 值,而这个值应该是它要调用的函数。在这种情况下,你可以使用bind() 来绑定call() 的this 值。在下面的代码片段中,slice() 是一个绑定了this 值为Array.prototype.slice() 的Function.prototype.call() 的版本。这意味着额外的call() 调用可以被省略:
// 与前面示例中的“slice”相同const unboundSlice = Array.prototype.slice;const slice = Function.prototype.call.bind(unboundSlice);// ...slice(arguments);规范
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-function.prototype.call> |