ë¹íì¤: ì´ ê¸°ë¥ì íì¤íëì´ ìì§ ììµëë¤. ë¹íì¤ ê¸°ë¥ì ë¸ë¼ì°ì ì§ìì´ ì íì ì´ë©° ë³ê²½ëê±°ë ì ê±°ë ì ìì¼ë¯ë¡ íë¡ëì íê²½ìì ì¬ì©íë ê²ì ê¶ì¥ëì§ ììµëë¤. ê·¸ë¬ë íì¤ ìµì ì´ ìë í¹ì ìí©ììë ì ì í ëìì´ ë ì ììµëë¤.
ì§ìì´ ì¤ë¨ëììµëë¤: ì´ ê¸°ë¥ì ë ì´ì ê¶ì¥ëì§ ììµëë¤. ì¼ë¶ ë¸ë¼ì°ì ììë ìì§ ì§ìí ì ìì§ë§ ê´ë ¨ ì¹ íì¤ìì ì´ë¯¸ ì ê±°ëìê±°ë ì ê±° ê³¼ì ì ìë ê²½ì°ê° ìì¼ë©°, í¸íì±ì ìí´ ì ì§ëê³ ìì ì ììµëë¤. ì¬ì©ì í¼íê³ ê°ë¥íë¤ë©´ 기존 ì½ë를 ì ë°ì´í¸íì¸ì. ê²°ì ì ì§ìí íë¨ì í¸íì± í를 íì¸íì¸ì. ì´ ê¸°ë¥ì ì¸ì ë ì§ ìëì ì¤ë¨í ì ììì ì ìíì¸ì.
ì°¸ê³ : ì격 모ëìì í¨ìì
caller
ì ì ê·¼íë©´ ì¤ë¥ê° ë°ìí©ëë¤. í´ë¹ APIê° ëì²´ëì§ ìê³ ì ê±°ëììµëë¤. ì´ë ë³´ì ìíì ì´ëíê³ ì¸ë¼ì´ë ë° ê¼¬ë¦¬ í¸ì¶(tail-call) ìµì íì ê°ì ìµì íì ê°ë¥ì±ì ì¬ê°íê² ì ííë ì½ëê° ì¤íì ìì°¨ì ì¼ë¡ ê²ì¬íë ê²(walk the stack)ì ë°©ì§í기 ìí ê²ì ëë¤. ìì¸í ì¤ëª ìarguments.callee
ì ì¬ì© ì¤ë¨ì ëí 근거ìì íì¸í ì ììµëë¤.
Function
ì¸ì¤í´ì¤ì caller
ì ê·¼ì ìì±ì ì´ í¨ì를 í¸ì¶í í¨ì를 ë°íí©ëë¤. ì격 모ë, íì´í, ë¹ë기 ë° ì ëë ì´í° í¨ìì ê²½ì° caller
ìì±ì ì¡ì¸ì¤íë©´ TypeError
ê° ë°ìí©ëë¤.
f
í¨ìê° ìµìì ì½ëì ìí´ í¸ì¶ë ê²½ì° f.caller
ì ê°ì null
ì´ê³ , ê·¸ë ì§ ìì¼ë©´ f
를 í¸ì¶í í¨ìì
ëë¤. f
를 í¸ì¶í í¨ìê° ì격 모ë í¨ìì¸ ê²½ì° f.caller
ì ê°ë null
ì
ëë¤.
ECMAScript ì¬ìì ì§ì ë ì ì¼í ëìì Function.prototype
ì 모ë get
ëë set
ìì²("í¬ì´ì¦ í ì ê·¼ì"(poison pill accessor)ë¼ê³ í¨)ì ëí´ ë¬´ì¡°ê±´ TypeError
ê° ë°ìíë ì´ê¸° caller
ì ê·¼ìê° ìë¤ë ê²ê³¼ 구íì²´ìì ì´ ì미를 ë³ê²½í ì ìë ì격íì§ ìì ì¼ë° í¨ì를 ì ì¸í ì´ë¤ í¨ììë ì격í 모ë í¨ì ê°ì´ ìì´ì¼ íë¤ë ì ì ì ìíì기 ë°ëëë¤. caller
ìì±ì ì¤ì ëìì ì¤ë¥ë¥¼ ë°ììí¤ë ê² ì´ì¸ì ê²½ì° êµ¬íì²´ì ë°ë¼ ì ìë©ëë¤. ì를 ë¤ì´ Chromeì ìì²´ ë°ì´í° ìì±ì¼ë¡ ì ìíë ë°ë©´, Firefoxì Safarië ì´ê¸° í¬ì´ì¦ í Function.prototype.caller
ì ê·¼ì를 íì¥íì¬ ì격í í¨ìê° ìë this
ê°ì í¹ë³í ì²ë¦¬í©ëë¤.
(function f() {
if (Object.hasOwn(f, "caller")) {
console.log(
"caller is an own property with descriptor",
Object.getOwnPropertyDescriptor(f, "caller"),
);
} else {
console.log(
"f doesn't have an own property named caller. Trying to get f.[[Prototype]].caller",
);
console.log(
Object.getOwnPropertyDescriptor(
Object.getPrototypeOf(f),
"caller",
).get.call(f),
);
}
})();
// In Chrome:
// caller is an own property with descriptor {value: null, writable: false, enumerable: false, configurable: false}
// In Firefox:
// f doesn't have an own property named caller. Trying to get f.[[Prototype]].caller
// null
ì´ ìì±ì arguments
ê°ì²´ì 미ì¬ì© arguments.caller
ìì±ì ëì²´í©ëë¤.
ìë ì½ëë í¨ìì caller
ìì± ê°ì íì¸í©ëë¤.
function myFunc() {
if (myFunc.caller === null) {
return "The function was called from the top!";
} else {
return `This function's caller was ${myFunc.caller}`;
}
}
ì¤í ì¬ìì±ê³¼ ì¬ê·
ì¬ê·ì ê²½ì°ìë ì´ ìì±ì ì¬ì©íì¬ í¸ì¶ ì¤íì ì¬ìì±í ì ììì ì ìíì기 ë°ëëë¤. ìë를 ê³ ë ¤í´ë³´ì¸ì.
function f(n) {
g(n - 1);
}
function g(n) {
if (n > 0) {
f(n);
} else {
stop();
}
}
f(2);
stop()
ì´ í¸ì¶ë ìì ììì í¸ì¶ ì¤íì ìëì ê°ìµëë¤.
f(2) -> g(1) -> f(1) -> g(0) -> stop()
ìëë ì°¸ì ëë¤.
stop.caller === g && f.caller === g && g.caller === f;
ìëì ê°ì´ stop()
í¨ììì ì¤í ì¶ì ì ê°ì ¸ì¤ë ¤ê³ ìëíë¤ë©´
let f = stop;
let stack = "Stack trace:";
while (f) {
stack += `\n${f.name}`;
f = f.caller;
}
루íë ì ë ë©ì¶ì§ ììµëë¤.
ì격 모ëììì callercallerê° ì격 모ë í¨ìë¼ë©´, caller
ì ê°ì null
ì
ëë¤.
function callerFunc() {
calleeFunc();
}
function strictCallerFunc() {
"use strict";
calleeFunc();
}
function calleeFunc() {
console.log(calleeFunc.caller);
}
(function () {
callerFunc();
})();
// Logs [Function: callerFunc]
(function () {
strictCallerFunc();
})();
// Logs null
ëª
ì¸ì
ì´ë¤ íì¤ìë ìíì§ ììµëë¤.
ë¸ë¼ì°ì í¸íì± ê°ì´ 보기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