此頁面由社群從英文翻譯而來。了解更多並加入 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月.
使用給定的this 參數以及分別給定的參數來呼叫某個函數
備註:此函數的所有語法大致上與apply() 相同,他們基本上不同處只有call() 接受一連串的參數,而apply() 單一的 array 作為參數
| Function物件的方法 | |
|---|---|
| 被實作於 | JavaScript 1.3 |
| ECMAScript 版本 | ECMAScript 第三版 |
In this article
語法
fun.call(thisArg[, arg1[, arg2[, ...]]])參數
thisArg呼叫*
fun*時提供的this值。 注意,它可能是一個無法在函數內看到的值:若這個函數是在非嚴苛模式(non-strict mode ),null、undefined將會被置換成全域變數,而原生型態的值將會被封裝arg1, arg2, ...其他參數
描述
你可以在呼叫一個現存的函數時,使用不一樣的this 物件。this 會參照到目前的物件,呼叫的物件上
使用call,你可以實作函數一次,然後在其他的物件上直接繼承它,而不用在新的物件上重寫該函數
範例
>使用call 來串接物件上的建構子
你可以使用call 來串接其他物件的建構子,就像 Java。下面的例子中,Product 物件的建構子定義了兩個參數name 以及price。其他函數Food 及Toy 引用了Product 並傳入this、name 和price。Product 初始化它的屬性name 和price,而兩個子函數則定義了category。
function Product(name, price) { this.name = name; this.price = price; if (price < 0) throw RangeError( 'Cannot create product "' + name + '" with a negative price', ); return this;}function Food(name, price) { Product.call(this, name, price); this.category = "food";}Food.prototype = new Product();function Toy(name, price) { Product.call(this, name, price); this.category = "toy";}Toy.prototype = new Product();var cheese = new Food("feta", 5);var fun = new Toy("robot", 40);使用call 來呼叫匿名的函數
下面這個簡易的例子中,我們做了一個匿名的函數,並用call 來讓它應用在每個在串列中的物件中. 這個匿名函數的主要用途是加入一個 print 函數到每個物件上,這個函數可以印出每個物件的 index 指標。 傳入物件作為this 的值並不是必要的,但他有解釋的用途。
var animals = [ { species: "Lion", name: "King" }, { species: "Whale", name: "Fail" },];for (var i = 0; i < animals.length; i++) { (function (i) { this.print = function () { console.log("#" + i + " " + this.species + ": " + this.name); }; this.print(); }).call(animals[i], i);}規範
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-function.prototype.call> |