[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Function/caller [Back]  [Original]

Function.prototype.caller - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Function.prototype.caller

Deprecated

Avoid using this feature in new projects. JavaScript strict mode prohibits accessing the caller and arguments properties of a Function object. This feature may be a candidate for removal from web standards or browsers.

: . . .

: caller . API . (tail-call) (walk the stack) . arguments.callee .

Function caller . , , caller TypeError .

In this article

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 .

js
(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

caller .

js
function myFunc() {
  if (myFunc.caller === null) {
    return "The function was called from the top!";
  } else {
    return `This function's caller was ${myFunc.caller}`;
  }
}

. .

js
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()

.

js
stop.caller === g && f.caller === g && g.caller === f;

stop()

js
let f = stop;
let stack = "Stack trace:";
while (f) {
  stack += `\n${f.name}`;
  f = f.caller;
}

.

caller

caller , caller null.

js
function callerFunc() {
  calleeFunc();
}

function strictCallerFunc() {
  "use strict";
  calleeFunc();
}

function calleeFunc() {
  console.log(calleeFunc.caller);
}

(function () {
  callerFunc();
})();
// Logs [Function: callerFunc]

(function () {
  strictCallerFunc();
})();
// Logs null

.


Web Proxy Viewer  |  New URL  |  Original Page