Visit Mozilla.org

Dokumentacja języka JavaScript 1.5:Obiekty:Function:caller

z Mozilla Developer Center, polskiego centrum programistów Mozilli.

UWAGA: Tłumaczenie tej strony nie zostało zakończone.
Może być ona niekompletna lub wymagać korekty.
Chcesz pomóc? | Dokończ tłumaczenie | Sprawdź ortografię | Więcej takich stron...

Spis treści

[edytuj] Podsumowanie

Określa funkcję, która powołuje się na aktualnie wykonywaną funkcje.

Ta własność nie jest częścią standardu ECMA-262 edycja 3. Zaimplementowana w najmniejszym stopniu jest w SpiderMonkey (JavaScriptowy silnik użyty w Mozilli) (zobacz błąd 65683) i JScript.

Własność obiektu: Function
Zaimplementowany w: JavaScript 1.5 (luty 2001)

[edytuj] Opis

Jeśli funkcja f została wywołana przez kod najwyższego poziomu, własność f.caller ma wartość null, w przeciwnym przypadku jest to funkcja, która wywołała f.

Ta własność zastąpiła wycofaną własność arguments.caller.

[edytuj] Notes

Note that in case of recursion, you can't reconstruct the call stack using this property. Consider:

function f(n) { g(n-1) }
function g(n) { if(n>0) f(n); else stop() }
f(2)

At the moment stop() is called the call stack will be:

f(2) -> g(1) -> f(1) -> g(0) -> stop()

The following is true:

stop.caller === g && f.caller === g && g.caller === f

so if you tried to get the stack trace in the stop() function like this:

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

the loop would never stop.

The special property __caller__, which returned the activation object of the caller thus allowing to reconstruct the stack, was removed for security reasons.

[edytuj] Przykłady

[edytuj] Przykład: Sprawdzenie wartości własności funkcji caller

Następujący kod sprawdza wartość własności funkcji caller.

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