aboutsummaryrefslogtreecommitdiff
path: root/files/pl/web/javascript/reference/global_objects/function/index.html
blob: 2db4d3341160f8914cb19c6413113d39529f2c1e (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
---
title: Function
slug: Web/JavaScript/Referencje/Obiekty/Function
tags:
  - Function
  - JavaScript
  - Konstruktor
translation_of: Web/JavaScript/Reference/Global_Objects/Function
---
<div>{{JSRef}}</div>

<p><strong>Konstruktor</strong> <strong><code>Function</code> </strong>tworzy nowy obiekt Function(tworzy funkcję poprzez konstruktor). W JavaScripcie właściwie każda funkcja jest obiektem <code>Function</code>.</p>

<h2 id="Syntax" name="Syntax">Składnia</h2>

<pre class="syntaxbox"><code>new Function ([<var>arg1</var>[, <var>arg2</var>[, ...<var>argN</var>]],] <var>functionBody</var>)</code></pre>

<h3 id="Parameters" name="Parameters">Parametry</h3>

<dl>
 <dt><code>arg1, arg2, ... arg<em>N</em></code></dt>
 <dd>Names to be used by the function as formal argument names. Each must be a string that corresponds to a valid JavaScript identifier or a list of such strings separated with a comma; for example "<code>x</code>", "<code>theValue</code>", or "<code>a,b</code>".</dd>
 <dt><code>functionBody</code></dt>
 <dd>A string containing the JavaScript statements comprising the function definition.</dd>
</dl>

<h2 id="Description" name="Description">Opis</h2>

<p><code>Function</code> objects created with the <code>Function</code> constructor are parsed when the function is created. This is less efficient than declaring a function with a <a href="/en-US/docs/Web/JavaScript/Reference/Operators/function">function expression</a> or <a href="/en-US/docs/Web/JavaScript/Reference/Statements/function">function statement</a> and calling it within your code, because such functions are parsed with the rest of the code.</p>

<p>All arguments passed to the function are treated as the names of the identifiers of the parameters in the function to be created, in the order in which they are passed.</p>

<div class="note">
<p><strong>Note:</strong> Functions created with the <code>Function</code> constructor do not create closures to their creation contexts; they always are created in the global scope. When running them, they will only be able to access their own local variables and global ones, not the ones from the scope in which the <code>Function</code> constructor was called. This is different from using {{jsxref("Global_Objects/eval", "eval")}} with code for a function expression.</p>
</div>

<p>Wywołanie konstruktora Function jako funkcję (bez użycia operatora 'new') ma taki sam efekt jak wywołanie konstruktora(z operatorem).</p>

<h2 id="Właściwości_i_metody_Function">Właściwości i metody <code>Function</code></h2>

<p>The global <code>Function</code> object has no methods or properties of its own, however, since it is a function itself it does inherit some methods and properties through the prototype chain from {{jsxref("Function.prototype")}}.</p>

<h2 id="Function_prototype_object"><code>Function</code> prototype object</h2>

<h3 id="Właściwości">Właściwości</h3>

<div>{{page('/en-US/docs/JavaScript/Reference/Global_Objects/Function/prototype', 'Properties')}}</div>

<h3 id="Methods" name="Methods">Metody</h3>

<div>{{page('/en-US/docs/JavaScript/Reference/Global_Objects/Function/prototype', 'Methods')}}</div>

<h2 id="Function_instances" name="Function_instances"><code>Function</code> instances</h2>

<p><code>Function</code> instances inherit methods and properties from {{jsxref("Function.prototype")}}. As with all constructors, you can change the constructor's prototype object to make changes to all <code>Function</code> instances.</p>

<h2 id="Examples" name="Examples">Przykłady</h2>

<h3 id="Example.3A_Specifying_arguments_with_the_Function_constructor" name="Example.3A_Specifying_arguments_with_the_Function_constructor">Przykład: Specifying arguments with the <code>Function</code> constructor</h3>

<p>Poniższy przykład tworzy obiekt Function(tworzy funkcję poprzez konstruktor), który przyjmuje dwa argumenty.</p>

<pre class="brush: js">// Przykład może być uruchomiony bezpośrednio w konsoli JavaScript

// Tworzy funkcję, która przyjmuje dwa argumenty i zwraca ich sumę
var adder = new Function('a', 'b', 'return a + b');

// Wywołanie funkcji
adder(2, 6);
// &gt; 8
</pre>

<p>Argumenty "<code>a</code>" i "<code>b</code>" są formanie nazwami argumentrów, które są użyte w ciele funkcji, "<code>return a + b</code>".</p>

<h3 id="Example:_A_recursive_shortcut_to_massively_modify_the_DOM" name="Example:_A_recursive_shortcut_to_massively_modify_the_DOM">Przykład: A recursive shortcut to massively modify the DOM</h3>

<p>Creating functions with the <code>Function</code> constructor is one of the ways to dynamically create an indeterminate number of new objects with some executable code into the global scope from a function. The following example (a recursive shortcut to massively modify the DOM) is impossible without the invocation of the <code>Function</code> constructor for each new query if you want to avoid closures.</p>

<pre class="brush: html  language-html"><code class="language-html">&lt;!doctype html&gt;
<span class="tag token"><span class="tag token"><span class="punctuation token">&lt;</span>html</span><span class="punctuation token">&gt;</span></span>
<span class="tag token"><span class="tag token"><span class="punctuation token">&lt;</span>head</span><span class="punctuation token">&gt;</span></span>
<span class="tag token"><span class="tag token"><span class="punctuation token">&lt;</span>meta</span> <span class="attr-name token">http-equiv</span><span class="attr-value token"><span class="punctuation token">=</span><span class="punctuation token">"</span>Content-Type<span class="punctuation token">"</span></span> <span class="attr-name token">content</span><span class="attr-value token"><span class="punctuation token">=</span><span class="punctuation token">"</span>text/html; charset<span class="punctuation token">=</span>UTF-8<span class="punctuation token">"</span></span> <span class="punctuation token">/&gt;</span></span>
<span class="tag token"><span class="tag token"><span class="punctuation token">&lt;</span>title</span><span class="punctuation token">&gt;</span></span>MDN Example - a recursive shortcut to massively modify the DOM<span class="tag token"><span class="tag token"><span class="punctuation token">&lt;/</span>title</span><span class="punctuation token">&gt;</span></span>
<span class="script token"><span class="tag token"><span class="tag token"><span class="punctuation token">&lt;</span>script</span> <span class="attr-name token">type</span><span class="attr-value token"><span class="punctuation token">=</span><span class="punctuation token">"</span>text/javascript<span class="punctuation token">"</span></span><span class="punctuation token">&gt;</span></span>
<span class="keyword token">var</span> domQuery <span class="operator token">=</span> <span class="punctuation token">(</span><span class="keyword token">function</span><span class="punctuation token">(</span><span class="punctuation token">)</span> <span class="punctuation token">{</span>
  <span class="keyword token">var</span> aDOMFunc <span class="operator token">=</span> <span class="punctuation token">[</span>
    Element<span class="punctuation token">.</span>prototype<span class="punctuation token">.</span>removeAttribute<span class="punctuation token">,</span>
    Element<span class="punctuation token">.</span>prototype<span class="punctuation token">.</span>setAttribute<span class="punctuation token">,</span>
    CSSStyleDeclaration<span class="punctuation token">.</span>prototype<span class="punctuation token">.</span>removeProperty<span class="punctuation token">,</span>
    CSSStyleDeclaration<span class="punctuation token">.</span>prototype<span class="punctuation token">.</span>setProperty
  <span class="punctuation token">]</span><span class="punctuation token">;</span>

  <span class="keyword token">function</span> <span class="function token">setSomething<span class="punctuation token">(</span></span>bStyle<span class="punctuation token">,</span> sProp<span class="punctuation token">,</span> sVal<span class="punctuation token">)</span> <span class="punctuation token">{</span>
    <span class="keyword token">var</span> bSet <span class="operator token">=</span> <span class="function token">Boolean<span class="punctuation token">(</span></span>sVal<span class="punctuation token">)</span><span class="punctuation token">,</span> fAction <span class="operator token">=</span> aDOMFunc<span class="punctuation token">[</span>bSet <span class="operator token">|</span> bStyle <span class="operator token">&lt;</span><span class="operator token">&lt;</span> <span class="number token">1</span><span class="punctuation token">]</span><span class="punctuation token">,</span>
        aArgs <span class="operator token">=</span> Array<span class="punctuation token">.</span>prototype<span class="punctuation token">.</span>slice<span class="punctuation token">.</span><span class="function token">call<span class="punctuation token">(</span></span>arguments<span class="punctuation token">,</span> <span class="number token">1</span><span class="punctuation token">,</span> bSet <span class="operator token">?</span> <span class="number token">3</span> <span class="punctuation token">:</span> <span class="number token">2</span><span class="punctuation token">)</span><span class="punctuation token">,</span>
        aNodeList <span class="operator token">=</span> bStyle <span class="operator token">?</span> <span class="keyword token">this</span><span class="punctuation token">.</span>cssNodes <span class="punctuation token">:</span> <span class="keyword token">this</span><span class="punctuation token">.</span>nodes<span class="punctuation token">;</span>

    <span class="keyword token">if</span> <span class="punctuation token">(</span>bSet <span class="operator token">&amp;&amp;</span> bStyle<span class="punctuation token">)</span> <span class="punctuation token">{</span> aArgs<span class="punctuation token">.</span><span class="function token">push<span class="punctuation token">(</span></span><span class="string token">''</span><span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="punctuation token">}</span>
    <span class="keyword token">for</span> <span class="punctuation token">(</span>
      <span class="keyword token">var</span> nItem <span class="operator token">=</span> <span class="number token">0</span><span class="punctuation token">,</span> nLen <span class="operator token">=</span> <span class="keyword token">this</span><span class="punctuation token">.</span>nodes<span class="punctuation token">.</span>length<span class="punctuation token">;</span>
      nItem <span class="operator token">&lt;</span> nLen<span class="punctuation token">;</span>
      fAction<span class="punctuation token">.</span><span class="function token">apply<span class="punctuation token">(</span></span>aNodeList<span class="punctuation token">[</span>nItem<span class="operator token">++</span><span class="punctuation token">]</span><span class="punctuation token">,</span> aArgs<span class="punctuation token">)</span>
    <span class="punctuation token">)</span><span class="punctuation token">;</span>
    <span class="keyword token">this</span><span class="punctuation token">.</span>follow <span class="operator token">=</span> setSomething<span class="punctuation token">.</span>caller<span class="punctuation token">;</span>
    <span class="keyword token">return</span> <span class="keyword token">this</span><span class="punctuation token">;</span>
  <span class="punctuation token">}</span>

  <span class="keyword token">function</span> <span class="function token">setStyles<span class="punctuation token">(</span></span>sProp<span class="punctuation token">,</span> sVal<span class="punctuation token">)</span> <span class="punctuation token">{</span> <span class="keyword token">return</span> setSomething<span class="punctuation token">.</span><span class="function token">call<span class="punctuation token">(</span></span><span class="keyword token">this</span><span class="punctuation token">,</span> <span class="keyword token">true</span><span class="punctuation token">,</span> sProp<span class="punctuation token">,</span> sVal<span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="punctuation token">}</span>
  <span class="keyword token">function</span> <span class="function token">setAttribs<span class="punctuation token">(</span></span>sProp<span class="punctuation token">,</span> sVal<span class="punctuation token">)</span> <span class="punctuation token">{</span> <span class="keyword token">return</span> setSomething<span class="punctuation token">.</span><span class="function token">call<span class="punctuation token">(</span></span><span class="keyword token">this</span><span class="punctuation token">,</span> <span class="keyword token">false</span><span class="punctuation token">,</span> sProp<span class="punctuation token">,</span> sVal<span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="punctuation token">}</span>
  <span class="keyword token">function</span> <span class="function token">getSelectors<span class="punctuation token">(</span></span><span class="punctuation token">)</span> <span class="punctuation token">{</span> <span class="keyword token">return</span> <span class="keyword token">this</span><span class="punctuation token">.</span>selectors<span class="punctuation token">;</span> <span class="punctuation token">}</span><span class="punctuation token">;</span>
  <span class="keyword token">function</span> <span class="function token">getNodes<span class="punctuation token">(</span></span><span class="punctuation token">)</span> <span class="punctuation token">{</span> <span class="keyword token">return</span> <span class="keyword token">this</span><span class="punctuation token">.</span>nodes<span class="punctuation token">;</span> <span class="punctuation token">}</span><span class="punctuation token">;</span>

  <span class="keyword token">return</span> <span class="punctuation token">(</span><span class="keyword token">function</span><span class="punctuation token">(</span>sSelectors<span class="punctuation token">)</span> <span class="punctuation token">{</span>
    <span class="keyword token">var</span> oQuery <span class="operator token">=</span> <span class="keyword token">new</span> <span class="class-name token">Function</span><span class="punctuation token">(</span><span class="string token">'return arguments.callee.follow.apply(arguments.callee, arguments);'</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
    oQuery<span class="punctuation token">.</span>selectors <span class="operator token">=</span> sSelectors<span class="punctuation token">;</span>
    oQuery<span class="punctuation token">.</span>nodes <span class="operator token">=</span> document<span class="punctuation token">.</span><span class="function token">querySelectorAll<span class="punctuation token">(</span></span>sSelectors<span class="punctuation token">)</span><span class="punctuation token">;</span>
    oQuery<span class="punctuation token">.</span>cssNodes <span class="operator token">=</span> Array<span class="punctuation token">.</span>prototype<span class="punctuation token">.</span>map<span class="punctuation token">.</span><span class="function token">call<span class="punctuation token">(</span></span>oQuery<span class="punctuation token">.</span>nodes<span class="punctuation token">,</span> <span class="keyword token">function</span><span class="punctuation token">(</span>oInlineCSS<span class="punctuation token">)</span> <span class="punctuation token">{</span> <span class="keyword token">return</span> oInlineCSS<span class="punctuation token">.</span>style<span class="punctuation token">;</span> <span class="punctuation token">}</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
    oQuery<span class="punctuation token">.</span>attributes <span class="operator token">=</span> setAttribs<span class="punctuation token">;</span>
    oQuery<span class="punctuation token">.</span>inlineStyle <span class="operator token">=</span> setStyles<span class="punctuation token">;</span>
    oQuery<span class="punctuation token">.</span>follow <span class="operator token">=</span> getNodes<span class="punctuation token">;</span>
    oQuery<span class="punctuation token">.</span>toString <span class="operator token">=</span> getSelectors<span class="punctuation token">;</span>
    oQuery<span class="punctuation token">.</span>valueOf <span class="operator token">=</span> getNodes<span class="punctuation token">;</span>
    <span class="keyword token">return</span> oQuery<span class="punctuation token">;</span>
  <span class="punctuation token">}</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
<span class="punctuation token">}</span><span class="punctuation token">)</span><span class="punctuation token">(</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
<span class="tag token"><span class="tag token"><span class="punctuation token">&lt;/</span>script</span><span class="punctuation token">&gt;</span></span></span>
<span class="tag token"><span class="tag token"><span class="punctuation token">&lt;/</span>head</span><span class="punctuation token">&gt;</span></span>

<span class="tag token"><span class="tag token"><span class="punctuation token">&lt;</span>body</span><span class="punctuation token">&gt;</span></span>

<span class="tag token"><span class="tag token"><span class="punctuation token">&lt;</span>div</span> <span class="attr-name token">class</span><span class="attr-value token"><span class="punctuation token">=</span><span class="punctuation token">"</span>testClass<span class="punctuation token">"</span></span><span class="punctuation token">&gt;</span></span>Lorem ipsum<span class="tag token"><span class="tag token"><span class="punctuation token">&lt;/</span>div</span><span class="punctuation token">&gt;</span></span>
<span class="tag token"><span class="tag token"><span class="punctuation token">&lt;</span>p</span><span class="punctuation token">&gt;</span></span>Some text<span class="tag token"><span class="tag token"><span class="punctuation token">&lt;/</span>p</span><span class="punctuation token">&gt;</span></span>
<span class="tag token"><span class="tag token"><span class="punctuation token">&lt;</span>div</span> <span class="attr-name token">class</span><span class="attr-value token"><span class="punctuation token">=</span><span class="punctuation token">"</span>testClass<span class="punctuation token">"</span></span><span class="punctuation token">&gt;</span></span>dolor sit amet<span class="tag token"><span class="tag token"><span class="punctuation token">&lt;/</span>div</span><span class="punctuation token">&gt;</span></span>

<span class="script token"><span class="tag token"><span class="tag token"><span class="punctuation token">&lt;</span>script</span> <span class="attr-name token">type</span><span class="attr-value token"><span class="punctuation token">=</span><span class="punctuation token">"</span>text/javascript<span class="punctuation token">"</span></span><span class="punctuation token">&gt;</span></span>
<span class="function token">domQuery<span class="punctuation token">(</span></span><span class="string token">'.testClass'</span><span class="punctuation token">)</span>
  <span class="punctuation token">.</span><span class="function token">attributes<span class="punctuation token">(</span></span><span class="string token">'lang'</span><span class="punctuation token">,</span> <span class="string token">'en'</span><span class="punctuation token">)</span><span class="punctuation token">(</span><span class="string token">'title'</span><span class="punctuation token">,</span> <span class="string token">'Risus abundat in ore stultorum'</span><span class="punctuation token">)</span>
  <span class="punctuation token">.</span><span class="function token">inlineStyle<span class="punctuation token">(</span></span><span class="string token">'background-color'</span><span class="punctuation token">,</span> <span class="string token">'black'</span><span class="punctuation token">)</span><span class="punctuation token">(</span><span class="string token">'color'</span><span class="punctuation token">,</span> <span class="string token">'white'</span><span class="punctuation token">)</span><span class="punctuation token">(</span><span class="string token">'width'</span><span class="punctuation token">,</span> <span class="string token">'100px'</span><span class="punctuation token">)</span><span class="punctuation token">(</span><span class="string token">'height'</span><span class="punctuation token">,</span> <span class="string token">'50px'</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
<span class="tag token"><span class="tag token"><span class="punctuation token">&lt;/</span>script</span><span class="punctuation token">&gt;</span></span></span>
<span class="tag token"><span class="tag token"><span class="punctuation token">&lt;/</span>body</span><span class="punctuation token">&gt;</span></span>

<span class="tag token"><span class="tag token"><span class="punctuation token">&lt;/</span>html</span><span class="punctuation token">&gt;</span></span></code></pre>

<p> </p>

<h2 id="Specyfikacja">Specyfikacja</h2>

<p> </p>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>ECMAScript 1st Edition.</td>
   <td>Standard</td>
   <td>Initial definition. Implemented in JavaScript 1.0.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.3', 'Function')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-function-objects', 'Function')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="Kompatybilność_z_przeglądarkami">Kompatybilność z przeglądarkami</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>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</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>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also" name="See_also">Zobacz również</h2>

<ul>
 <li>{{jsxref("Functions_and_function_scope", "Functions and function scope")}}</li>
 <li>{{jsxref("Function")}}</li>
 <li>{{jsxref("Statements/function", "function statement")}}</li>
 <li>{{jsxref("Operators/function", "function expression")}}</li>
 <li>{{jsxref("Statements/function*", "function* statement")}}</li>
 <li>{{jsxref("Operators/function*", "function* expression")}}</li>
 <li>{{jsxref("GeneratorFunction")}}</li>
</ul>