diff options
Diffstat (limited to 'files/pt-br/web/javascript/reference/global_objects/function/caller/index.html')
-rw-r--r-- | files/pt-br/web/javascript/reference/global_objects/function/caller/index.html | 129 |
1 files changed, 129 insertions, 0 deletions
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 +--- +<div>{{JSRef}} {{non-standard_header}}</div> + +<p>A propriedade <code><strong><em>function</em>.caller</strong></code> retorna a função que invocou a função especificada.</p> + +<h2 id="Descrição">Descrição</h2> + +<p>Se a função <code>f</code> foi invocada pelo codigo mais alto nível, o valor de <code>f.caller</code> é {{jsxref("null")}}, caso contrario, o valor será a função a qual invocou <code>f</code>.</p> + +<p>Esta propriedade substitui a propriedade obsoleta {{jsxref("Functions/arguments/caller", "arguments.caller")}} do objeto {{jsxref("Functions/arguments", "arguments")}}.</p> + +<p>A propriedade especial <code>__caller__</code>, a qual retornou o objeto de ativação do chamador, permitindo assin reconstruir o stack, foi removido por motivo de segurança.</p> + +<h3 id="Notas">Notas</h3> + +<p>Note que no caso de recurção, você não pode reconstruir o stack de chamadas usando esta propriedade. Considere:</p> + +<pre class="brush: js">function f(n) { g(n - 1); } +function g(n) { if (n > 0) { f(n); } else { stop(); } } +f(2); +</pre> + +<p>No momento em que <code>stop()</code> é chamado o stack será:</p> + +<pre class="brush: js">f(2) -> g(1) -> f(1) -> g(0) -> stop() +</pre> + +<p>O seguinte é true:</p> + +<pre class="brush: js">stop.caller === g && f.caller === g && g.caller === f +</pre> + +<p>então se você tentou recuperar o stack trace na função <code>stop()</code> assim:</p> + +<pre class="brush: js">var f = stop; +var stack = 'Stack trace:'; +while (f) { + stack += '\n' + f.name; + f = f.caller; +} +</pre> + +<p>o loop nunca irá parar.</p> + +<h2 id="Exemplos">Exemplos</h2> + +<h3 id="Verificando_o_valor_da_propriedade_caller_de_uma_função">Verificando o valor da propriedade <code>caller</code> de uma função</h3> + +<p>O código a seguir verifica o valor da propriedade <code>caller</code> de uma função.</p> + +<pre class="brush: js">function myFunc() { + if (myFunc.caller == null) { + return 'The function was called from the top!'; + } else { + return 'This function\'s caller was ' + myFunc.caller; + } +} +</pre> + +<h2 id="Especificações">Especificações</h2> + +<p>Não faz parte de nenhuma especificação. Implementado no JavaScript 1.5.</p> + +<h2 id="Compatibilidade_com_os_navegadores">Compatibilidade com os navegadores</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Suporte básico</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoDesktop("1.0")}}</td> + <td>8.0</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Chrome for Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Suporte básico</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoMobile("1.0")}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="Ver_também">Ver também</h2> + +<ul> + <li>Implementation bug for SpiderMonkey {{bug(65683)}}</li> +</ul> |