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æ.
call() 㯠Function ã¤ã³ã¹ã¿ã³ã¹ã®ã¡ã½ããã§ã this ã®å¤ã¨ãç¬ç«ãã¦æä¾ããã弿°ã«ãã£ã¦é¢æ°ãå¼ã³åºãã¾ãã
試ãã¦ã¿ã¾ããã
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);
// æå¾
ãããåºåçµæ: "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() ã§ã¯ 1 ã¤ã®ãªãã¸ã§ã¯ããé常ã¯é
åã«ã¾ã¨ãããã¾ããä¾ãã°ã 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 ãªãã¸ã§ã¯ãã«ãã¤ã³ããã¦ãã¾ãã
function greet() {
console.log(this.animal, "typically sleep between", this.sleepDuration);
}
const obj = {
animal: "cats",
sleepDuration: "12 and 16 hours",
};
greet.call(obj); // cats typically sleep between 12 and 16 hours
call() ã使ç¨ããæåã®å¼æ°ãæå®ããªã颿°ã®å¼ã³åºã
æåã® thisArg 弿°ãçç¥ããã¨ãæ¢å®ã§ã¯ undefined ã¨ãªãã¾ãã 峿 ¼ã¢ã¼ã以å¤ã§ã¯ã this ã®å¤ã¯ globalThis ï¼ã°ãã¼ãã«ãªãã¸ã§ã¯ãã«é¡ä¼¼ï¼ã«ç½®ãæãããã¾ãã
globalThis.globProp = "foo";
function display() {
console.log(`globProp ã®å¤ã¯ ${this.globProp}`);
}
display.call(); // "globProp ã®å¤ã¯ foo" ã¨åºå
峿 ¼ã¢ã¼ãã§ã¯ this ã®å¤ã¯ undefined ã«ãªãã¾ãã以ä¸ãåç
§ãã¦ãã ããã
"use strict";
globalThis.globProp = "foo";
function display() {
console.log(`globProp ã®å¤ã¯ ${this.globProp}`);
}
display.call(); // throws TypeError: Cannot read the property of 'globProp' of undefined
ã¡ã½ããã®ã¦ã¼ãã£ãªãã£é¢æ°ã¸ã®å¤æ
call() ã¯é常ã®é¢æ°å¼ã³åºãã¨ã»ã¼åæ§ã§ããã this ã颿°ã®ã¢ã¯ã»ã¹å
ã®å¤ã§ã¯ãªããé常ã®å¼æ°ã¨ãã¦æ¸¡ãããç¹ãç°ãªãã¾ããããã¯ãæ±ç¨ã¦ã¼ãã£ãªãã£é¢æ°ãã©ã®ããã«åä½ããã®ãã¨ä¼¼ã¦ãã¾ãã array.map(callback) ãå¼ã¶ä»£ããã«ã map(array, callback) ã使ç¨ãããã¨ã§ã Object.prototype ã夿´ãããã¨ãªããé
åã§ã¯ãªãé
å風ãªãã¸ã§ã¯ãï¼ä¾ãã° argumentsï¼ã«å¯¾ã㦠map ã使ç¨ãããã¨ãã§ãã¾ãã
ä¾ãã°ãé
å風ã®ãªãã¸ã§ã¯ããå®éã®é
åã«å¤æããããã«ä½¿ç¨ãã Array.prototype.slice() ãä¾ã«æãã¾ãã ãã®ä¾ã§ã¯ã次ã®ãããªã·ã§ã¼ãã«ããã使ãããã¨ãã§ãã¾ãã
const slice = Array.prototype.slice;
// â¦
slice.call(arguments);
ãªãã slice.call ãä¿åãã¦ããããåãªã颿°ã¨ãã¦åä»ãããã¨ã¯ã§ãã¾ããã call() ã¡ã½ããããã®å¤ãèªã¿åããããå¼ã³åºãã¹ã颿°ãæå®ããã¦ãã¾ãããã§ãããã®å ´åã bind() ã使ç¨ãã㨠this å¤ã call() ã«ãã¤ã³ããããã¨ãã§ãã¾ããæ¬¡ã®ã³ã¼ãã§ã¯ã slice() 㯠Function.prototype.call() ã®ãã¤ã³ãæ¸ã¿ãã¼ã¸ã§ã³ã§ããã this å¤ã¯ Array.prototype.slice() ã«ãã¤ã³ãããã¦ãã¾ããã¤ã¾ãã追å ã® call() å¼ã³åºããçç¥ã§ããã¨ãããã¨ã§ãã
// åã®ä¾ã® "slice" ã¨åã
const unboundSlice = Array.prototype.slice;
const slice = Function.prototype.call.bind(unboundSlice);
// â¦
slice(arguments);
仿§æ¸
| Specification |
|---|
| ECMAScript® 2027 Language Specification> # sec-function.prototype.call> |