Baseline Widely available
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
ì ê°.
ë°íê°(Return Value)ì°¸ê³ :
this
ë ë©ìëì ìí´ ë³´ì´ë ì¤ì ê°ì´ ìë ì ììì 주ìíì¸ì: ë©ìëê° ë¹ì격 모ë ì½ë ë´ í¨ìì¸ ê²½ì°,null
ë°undefined
ë ì ì ê°ì²´ë¡ ëì²´ëê³ ììê°ì ê°ì²´ë¡ ë³íë©ëë¤.arg1, arg2, ...
ê°ì²´ë¥¼ ìí ì¸ì.
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
ëª
ì¸ì ë¸ë¼ì°ì í¸íì± ì°¸ì¡°
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4