aboutsummaryrefslogtreecommitdiff
path: root/files/ca/web/javascript/reference/global_objects/function/caller/index.html
blob: 613e163d6a541a768c2748a5fc99f4e32e9ef2f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
---
title: Function.caller
slug: Web/JavaScript/Reference/Global_Objects/Function/caller
translation_of: Web/JavaScript/Reference/Global_Objects/Function/caller
---
<div>{{JSRef}} {{non-standard_header}}</div>

<p>La propietat <code><strong><em>function</em>.caller</strong></code> retorna la funció que ha invocat la funció especificada.</p>

<h2 id="Descripció">Descripció</h2>

<p>SI la funció <code>f</code> ha estat invocada pel codi de nivell superior, el valor de <code>f.caller</code> és {{jsxref("null")}}, sinó és la funció que ha cridat <code>f</code>.</p>

<p>Aquesta propietat reemplaça la propietat obsoleta {{jsxref("Functions/arguments/caller", "arguments.caller")}} de l'objecte {{jsxref("Functions/arguments", "arguments")}}.</p>

<p>La propietat especial <code>__caller__</code>, la qual retorna l'objecte activatwhich returned the activation object of the caller thus allowing to reconstruct the stack, was removed for security reasons.</p>

<h3 id="Notes">Notes</h3>

<p>Vegeu que en cas de recursió, no podeu reconstruir la pila de crida fent servir aquesta propietat. C that in case of recursion, you can't reconstruct the call stack using this property. Tingueu en compte:</p>

<pre class="brush: js">function f(n) { g(n - 1); }
function g(n) { if (n &gt; 0) { f(n); } else { stop(); } }
f(2);
</pre>

<p>At the moment <code>stop()</code> is called the call stack will be:</p>

<pre class="brush: js">f(2) -&gt; g(1) -&gt; f(1) -&gt; g(0) -&gt; stop()
</pre>

<p>El següent és cert:</p>

<pre class="brush: js">stop.caller === g &amp;&amp; f.caller === g &amp;&amp; g.caller === f
</pre>

<p>so if you tried to get the stack trace in the <code>stop()</code> function like this:</p>

<pre class="brush: js">var f = stop;
var stack = 'Stack trace:';
while (f) {
  stack += '\n' + f.name;
  f = f.caller;
}
</pre>

<p>El bucle mai s'aturaria.</p>

<h2 id="Exemples">Exemples</h2>

<h3 id="Checking_the_value_of_a_function's_caller_property">Checking the value of a function's <code>caller</code> property</h3>

<p>El codi següent comprova que el valor following code checks the value a function's <code>caller</code> property.</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="Especificacions">Especificacions</h2>

<p>No forma part de cap especificació. Implementat en JavaScript 1.5.</p>

<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2>

<div>{{CompatibilityTable}}</div>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Característica</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Suport bàsic</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>Característica</th>
   <th>Android</th>
   <th>Chrome per Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Suport bàsic</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="Vegeu_també">Vegeu també</h2>

<ul>
 <li>Error d'Implementació de SpiderMonkey {{bug(65683)}}</li>
</ul>