Files
uTools-Manuals/docs/javascript/Reference/Global_Objects/Function/caller.html

46 lines
3.5 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<article id="wikiArticle">
<div> <div class="blockIndicator nonStandard nonStandardHeader">
<p><strong><span class="icon-only-inline" title="This API has not been standardized."><i class="icon-warning-sign"> </i></span> 非标准</strong><br/>
该特性是非标准的,请尽量不要在生产环境中使用它!</p>
</div></div>
<h2 id="Summary" name="Summary">概述</h2>
<p>返回调用指定函数的函数.</p>
<p>该属性不是ECMA-262第3版标准的一部分.不过, <a href="/zh-cn/SpiderMonkey" title="zh-cn/SpiderMonkey">SpiderMonkey</a> (Mozilla的JavaScript引擎) (查看<a class="external" href="https://bugzilla.mozilla.org/show_bug.cgi?id=65683" rel="noopener" title="FIXED: Function objects should have a caller property">bug 65683</a>), V8 (Chrome的JavaScript引擎) 和 JScript(IE的ECMAScript实现)都已经支持了它.</p>
<h2 id="Description" name="Description">描述</h2>
<p>如果一个函数<code>f</code>是在全局作用域内被调用的,则<code>f.caller为</code><code>null</code>,相反,如果一个函数是在另外一个函数作用域内被调用的,则<code>f.caller指向调用它的那个函数.</code></p>
<p>该属性的常用形式<code>arguments.callee.caller</code>替代了被废弃的 <a href="/zh-cn/JavaScript/Reference/Functions_and_function_scope/arguments/caller" title="zh-cn/JavaScript/Reference/Functions_and_function_scope/arguments/caller">arguments.caller</a>.</p>
<h3 id="Notes" name="Notes">备注</h3>
<p>注意,在使用递归调用时, 你不能使用此属性来重现出调用栈.请考虑以下代码:</p>
<pre><code class="language-javascript">function f(n) { g(n-1) }
function g(n) { if(n&gt;0) f(n); else stop() }
f(2)
</code></pre>
<p><code>stop()函数被调用时,调用栈是这样的</code>:</p>
<pre><code class="language-javascript">f(2) -&gt; g(1) -&gt; f(1) -&gt; g(0) -&gt; stop()
</code></pre>
<p>由于下面的表达式为 true(只保留函数最后一次被调用时的caller):</p>
<pre><code class="language-javascript">stop.caller === g &amp;&amp; f.caller === g &amp;&amp; g.caller === f
</code></pre>
<p>所以如果你尝试在<code>stop()</code>函数中获取调用栈的话:</p>
<pre><code class="language-javascript">var f = stop;
var stack = "调用栈:";
while (f) {
stack += "\n" + f.name;
f = f.caller;
}
</code></pre>
<p>则上面的代码会进入一个死循环.</p>
<p>有一个特殊属性 <code>__caller__</code>, 可以返回调用当前函数的函数的活动对象(可以用来重现出整个调用栈), 但由于安全原因的考虑,该属性已被删除.</p>
<h2 id="Examples" name="Examples">例子</h2>
<h3 id="Example:_Checking_the_value_of_a_function.27s_caller_property" name="Example:_Checking_the_value_of_a_function.27s_caller_property">例子: 检测一个函数的<code>caller</code>属性的值</h3>
<p>下例用来得出一个函数是被谁调用的<code>.</code></p>
<pre><code class="language-javascript">function myFunc() {
if (myFunc.caller == null) {
return ("<span><span class="string">该函数在全局作用域内被调用</span></span>!");
} else
return ("调用我的是函数是" + myFunc.caller);
}
</code></pre>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<p>Function.caller目前被所有主流浏览器支持: Firefox, Safari, Chrome, Opera 和 IE. <a class="external" href="http://dl.dropbox.com/u/534786/callertest.html" rel="noopener" title="http://dl.dropbox.com/u/534786/callertest.html"><span style="text-decoration: underline;">查看检测结果</span></a>.</p>
</article>