From 074785cea106179cb3305637055ab0a009ca74f2 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:42:52 -0500 Subject: initial commit --- .../global_objects/function/caller/index.html | 129 +++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 files/pt-br/web/javascript/reference/global_objects/function/caller/index.html (limited to 'files/pt-br/web/javascript/reference/global_objects/function/caller/index.html') diff --git a/files/pt-br/web/javascript/reference/global_objects/function/caller/index.html b/files/pt-br/web/javascript/reference/global_objects/function/caller/index.html new file mode 100644 index 0000000000..c380d89d7f --- /dev/null +++ b/files/pt-br/web/javascript/reference/global_objects/function/caller/index.html @@ -0,0 +1,129 @@ +--- +title: Function.caller +slug: Web/JavaScript/Reference/Global_Objects/Function/caller +tags: + - Função + - JavaScript + - Non-standard + - Propriedades +translation_of: Web/JavaScript/Reference/Global_Objects/Function/caller +--- +
{{JSRef}} {{non-standard_header}}
+ +

A propriedade function.caller retorna a função que invocou a função especificada.

+ +

Descrição

+ +

Se a função f foi invocada pelo codigo mais alto nível, o valor de f.caller é {{jsxref("null")}}, caso contrario, o valor será a função a qual invocou f.

+ +

Esta propriedade substitui a propriedade obsoleta {{jsxref("Functions/arguments/caller", "arguments.caller")}} do objeto {{jsxref("Functions/arguments", "arguments")}}.

+ +

A propriedade especial __caller__, a qual retornou o objeto de ativação do chamador, permitindo assin reconstruir o stack, foi removido por motivo de segurança.

+ +

Notas

+ +

Note que no caso de recurção, você não pode reconstruir o stack de chamadas usando esta propriedade. Considere:

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

No momento em que stop() é chamado o stack será:

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

O seguinte é true:

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

então se você tentou recuperar o stack trace na função stop() assim:

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

o loop nunca irá parar.

+ +

Exemplos

+ +

Verificando o valor da propriedade caller de uma função

+ +

O código a seguir verifica o valor da propriedade caller de uma função.

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

Especificações

+ +

Não faz parte de nenhuma especificação. Implementado no JavaScript 1.5.

+ +

Compatibilidade com os navegadores

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Suporte básico{{CompatVersionUnknown}}{{CompatGeckoDesktop("1.0")}}8.0{{CompatVersionUnknown}}{{CompatVersionUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Suporte básico{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatGeckoMobile("1.0")}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
+
+ +

Ver também

+ + -- cgit v1.2.3-54-g00ecf