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() ë©ìëë 주ì´ì§ this ê° ë° ê°ê° ì ë¬ë ì¸ìì í¨ê» í¨ì를 í¸ì¶í©ëë¤.
ì°¸ê³ :
ì´ í¨ì 구문ì apply()ì ê±°ì ëì¼íì§ë§, call()ì ì¸ì 목ë¡ì, ë°ë©´ì apply()ë ì¸ì ë°°ì´ íë를 ë°ëë¤ë ì ì´ ì¤ìí ì°¨ì´ì ì
ëë¤.
ìëí´ ë³´ê¸°
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"
구문
func.call(thisArg[, arg1[, arg2[, ...]]])
매ê°ë³ì
thisArg-
funcí¸ì¶ì ì ê³µëëthisì ê°.
ì°¸ê³ :
thisë ë©ìëì ìí´ ë³´ì´ë ì¤ì ê°ì´ ìë ì ììì 주ìíì¸ì: ë©ìëê° ë¹ì격 모ë ì½ë ë´ í¨ìì¸ ê²½ì°, null ë° undefinedë ì ì ê°ì²´ë¡ ëì²´ëê³ ììê°ì ê°ì²´ë¡ ë³íë©ëë¤. arg1, arg2, ... ê°ì²´ë¥¼ ìí ì¸ì.
ë°íê°(Return Value)
this ì arguments 를 매ê°ë¡ í¸ì¶ë í¨ìì ë°íê°
ì¤ëª
call()ì ì´ë¯¸ í ë¹ëì´ìë ë¤ë¥¸ ê°ì²´ì í¨ì/ë©ìë를 í¸ì¶íë í´ë¹ ê°ì²´ì ì¬í ë¹í ë ì¬ì©ë©ëë¤. thisë íì¬ ê°ì²´(í¸ì¶íë ê°ì²´)를 참조í©ëë¤. ë©ìë를 íë² ìì±íë©´ ì ê°ì²´ë¥¼ ìí ë©ìë를 ì¬ìì±í íì ìì´ call()ì ì´ì©í´ ë¤ë¥¸ ê°ì²´ì ììí ì ììµëë¤.
ì
>ê°ì²´ì ìì±ì ì°ê²°ì call ì¬ì©
Javaì ë¹ì·íê², ê°ì²´ì ìì±ì ì°ê²°(chain)ì callì ì¬ì©í ì ììµëë¤. ë¤ì ììì, Product ê°ì²´ì ìì±ìë name ë° price 를 매ê°ë³ìë¡ ì ìë©ëë¤. ë¤ë¥¸ ë í¨ì Food ë° Toyë this ë° nameê³¼ price를 ì ë¬íë Product를 í¸ì¶í©ëë¤. Productë name ë° price ìì±ì ì´ê¸°ííê³ , í¹ìí ë í¨ì(Food ë° Toy)ë category를 ì ìí©ëë¤.
function Product(name, price) {
this.name = name;
this.price = price;
if (price < 0) {
throw RangeError(
"Cannot create product " + this.name + " with a negative price",
);
}
}
function Food(name, price) {
Product.call(this, name, price);
this.category = "food";
}
function Toy(name, price) {
Product.call(this, name, price);
this.category = "toy";
}
var cheese = new Food("feta", 5);
var fun = new Toy("robot", 40);
ìµëª
í¨ì í¸ì¶ì call ì¬ì©
ì´ ìì ììë ìµëª
í¨ì를 ë§ë¤ê³ ë°°ì´ ë´ ëª¨ë ê°ì²´ìì ì´ë¥¼ í¸ì¶í기 ìí´ callì ì¬ì©í©ëë¤. ì¬ê¸°ì ìµëª
í¨ìì 주목ì ì ë°°ì´ ë´ ê°ì²´ì ì íí ì¸ë±ì¤ë¥¼ ì¶ë ¥í ì ìë 모ë ê°ì²´ì print í¨ì를 ì¶ê°íë ê² ì
ëë¤.
ì°¸ê³ :
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);
}
í¨ì í¸ì¶ ë° 'this'를 ìí 문맥 ì§ì ì call ì¬ì©
ìë ìì ìì, greetì í¸ì¶íë©´ this ê°ì ê°ì²´ objì ë°ì¸ë©ë©ëë¤.
function greet() {
var reply = [this.animal, "typically sleep between", this.sleepDuration].join(
" ",
);
console.log(reply);
}
var obj = {
animal: "cats",
sleepDuration: "12 and 16 hours",
};
greet.call(obj); // cats typically sleep between 12 and 16 hours
첫ë²ì§¸ ì¸ì ì§ì ìì´ í¨ì í¸ì¶ì call ì¬ì©
ìë ìì ìì, display í¨ìì 첫ë²ì§¸ ì¸ì를 ì ë¬íì§ ìê³ í¸ì¶í©ëë¤. 첫ë²ì§¸ ì¸ì를 ì ë¬íì§ ìì¼ë©´, thisì ê°ì ì ì ê°ì²´ì ë°ì¸ë©ë©ëë¤.
var sData = "Wisen";
function display() {
console.log("sData value is %s ", this.sData);
}
display.call(); // sData value is Wisen
ì°¸ê³ :
ì격 모ë(strict mode)ìì, this ë undefinedê°ì ê°ì§ëë¤. See below.
"use strict";
var sData = "Wisen";
function display() {
console.log("sData value is %s ", this.sData);
}
display.call(); // Cannot read the property of 'sData' of undefined
ëª ì¸ì
| Specification |
|---|
| ECMAScript® 2027 Language Specification> # sec-function.prototype.call> |