aboutsummaryrefslogtreecommitdiff
path: root/files/ru/web/javascript/reference/global_objects/generatorfunction/index.html
blob: 4cce9504e9310fd92a5ccb5c0b4bd0abad652d09 (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
---
title: GeneratorFunction
slug: Web/JavaScript/Reference/Global_Objects/GeneratorFunction
translation_of: Web/JavaScript/Reference/Global_Objects/GeneratorFunction
---
<div>{{JSRef}}</div>

<p><strong><code>GeneratorFunction</code> constructor</strong> создает новый {{jsxref("Statements/function*", "generator function")}} объект. В JavaScript каждая функция-генератор - это фактически <code>GeneratorFunction</code> объект.</p>

<p><code>Обратите внимание, что GeneratorFunction</code> - это не глобальный объект. Он может быть получен при выполнении следующего кода.</p>

<pre class="brush: js">Object.getPrototypeOf(function*(){}).constructor
</pre>

<h2 id="Синтаксис">Синтаксис</h2>

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

<h3 id="Параметры">Параметры</h3>

<dl>
 <dt><code>arg1, arg2, ... arg<em>N</em></code></dt>
 <dd>Имена, используемые функцией как имена формальных аргументов. Каждый должен быть строкой, которая соответствует правильному JavaScript идентификатору или списком таких строк, разделенных запятыми; например "<code>x</code>", "<code>theValue</code>", или "<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">Description</h2>

<p>{{jsxref("Statements/function*", "generator function")}} objects created with the <code>GeneratorFunction</code> constructor are parsed when the function is created. This is less efficient than declaring a generator function with a {{jsxref("Statements/function*", "function* expression")}} 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> {{jsxref("Statements/function*", "generator function")}} created with the <code>GeneratorFunction</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>GeneratorFunction</code> constructor was called. This is different from using {{jsxref("Global_Objects/eval", "eval")}} with code for a generator function expression.</p>
</div>

<p>Invoking the <code>GeneratorFunction</code> constructor as a function (without using the <code>new</code> operator) has the same effect as invoking it as a constructor.</p>

<h2 id="Properties">Properties</h2>

<dl>
 <dt><code><strong>GeneratorFunction.length</strong></code></dt>
 <dd>The <code>GeneratorFunction</code> constructor's length property whose value is 1.</dd>
 <dt>{{jsxref("GeneratorFunction.prototype")}}</dt>
 <dd>Allows the addition of properties to all generator function objects.</dd>
</dl>

<h2 id="GeneratorFunction_prototype_object"><code>GeneratorFunction</code> prototype object</h2>

<h3 id="Properties_2">Properties</h3>

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

<h2 id="GeneratorFunction_instances"><code>GeneratorFunction</code> instances</h2>

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

<h2 id="Examples">Examples</h2>

<h3 id="Creating_a_generator_function_from_a_GeneratorFunction_constructor">Creating a generator function from a <code>GeneratorFunction</code> constructor</h3>

<pre class="brush: js">var GeneratorFunction = Object.getPrototypeOf(function*(){}).constructor
var g = new GeneratorFunction('a', 'yield a * 2');
var iterator = g(10);
console.log(iterator.next().value); // 20
</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES2015', '#sec-generatorfunction-objects', 'GeneratorFunction')}}</td>
   <td>{{Spec2('ES2015')}}</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-generatorfunction-objects', 'GeneratorFunction')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<div>


<p>{{Compat("javascript.builtins.GeneratorFunction")}}</p>
</div>

<h2 id="See_also">See also</h2>

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