diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/javascript/reference/global_objects/function/caller | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/function/caller')
-rw-r--r-- | files/zh-cn/web/javascript/reference/global_objects/function/caller/index.html | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/function/caller/index.html b/files/zh-cn/web/javascript/reference/global_objects/function/caller/index.html new file mode 100644 index 0000000000..a850d41582 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/function/caller/index.html @@ -0,0 +1,69 @@ +--- +title: Function.caller +slug: Web/JavaScript/Reference/Global_Objects/Function/caller +translation_of: Web/JavaScript/Reference/Global_Objects/Function/caller +--- +<div>{{JSRef("Global_Objects", "Function")}} {{non-standard_header}}</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引擎) (查看{{ Bug("65683") }}), 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 class="brush: js">function f(n) { g(n-1) } +function g(n) { if(n>0) f(n); else stop() } +f(2) +</pre> + +<p>当<code>stop()函数被调用时,调用栈是这样的</code>:</p> + +<pre class="brush: js">f(2) -> g(1) -> f(1) -> g(0) -> stop() +</pre> + +<p>由于下面的表达式为 true(只保留函数最后一次被调用时的caller):</p> + +<pre class="brush: js">stop.caller === g && f.caller === g && g.caller === f +</pre> + +<p>所以如果你尝试在<code>stop()</code>函数中获取调用栈的话:</p> + +<pre class="brush: js">var f = stop; +var stack = "调用栈:"; +while (f) { + stack += "\n" + f.name; + f = f.caller; +} +</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 class="brush: js">function myFunc() { + if (myFunc.caller == null) { + return ("<span><span class="string">该函数在全局作用域内被调用</span></span>!"); + } else + return ("调用我的是函数是" + myFunc.caller); +} +</pre> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>Function.caller目前被所有主流浏览器支持: Firefox, Safari, Chrome, Opera 和 IE. <a class="external" href="http://dl.dropbox.com/u/534786/callertest.html" title="http://dl.dropbox.com/u/534786/callertest.html"><span style="text-decoration: underline;">查看检测结果</span></a>.</p> |