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/generatorfunction | |
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/generatorfunction')
-rw-r--r-- | files/zh-cn/web/javascript/reference/global_objects/generatorfunction/index.html | 113 | ||||
-rw-r--r-- | files/zh-cn/web/javascript/reference/global_objects/generatorfunction/prototype/index.html | 64 |
2 files changed, 177 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/generatorfunction/index.html b/files/zh-cn/web/javascript/reference/global_objects/generatorfunction/index.html new file mode 100644 index 0000000000..ab93243b97 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/generatorfunction/index.html @@ -0,0 +1,113 @@ +--- +title: GeneratorFunction +slug: Web/JavaScript/Reference/Global_Objects/GeneratorFunction +tags: + - Constructor + - ECMAScript 2015 + - GeneratorFunction + - Iterator + - JavaScript + - Reference +translation_of: Web/JavaScript/Reference/Global_Objects/GeneratorFunction +--- +<div>{{JSRef("Global_Objects", "生成器函数")}}</div> + +<p><strong><code>GeneratorFunction</code>构造器</strong>生成新的{{jsxref("Statements/function*", "生成器函数")}} 对象。在JavaScript中,生成器函数实际上都是<code>GeneratorFunction</code>的实例对象。</p> + +<p>注意,<code>GeneratorFunction</code>并不是一个全局对象。它可以通过下面的代码获取。</p> + +<pre class="brush: js">Object.getPrototypeOf(function*(){}).constructor +</pre> + +<h2 id="Syntax" name="Syntax">语法</h2> + +<pre class="syntaxbox"><code>new GeneratorFunction ([<var>arg1</var>[, <var>arg2</var>[, ...<var>argN</var>]],] <var>functionBody</var>)</code></pre> + +<h3 id="Parameters" name="Parameters">参数</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>一个包含多条表示JavaScript函数体语句的字符串。</dd> +</dl> + +<h2 id="Description" name="Description">描述</h2> + +<p>当创建函数时,将使用<code>GeneratorFunction</code>构造函数创建的{{jsxref("Statements/function*", "生成器函数")}}对象进行解析。这比使用{{jsxref("Statements/function*", "function* 表达式")}} 声明生成器函数效率更低,并且在代码中调用它,因为这些函数与其余的代码一起被解析。</p> + +<p>传递给函数的所有参数按照它们被传递的顺序被视为要创建的函数中参数的标识符的名称。</p> + +<div class="note"> +<p><strong>提示:</strong>使用<code>GeneratorFunction</code>构造函数创建的{{jsxref("Statements/function*", "生成器函数")}}不会为其创建上下文创建闭包;它们始终在全局范围内创建。当运行它们时,它们只能访问自己的本地变量和全局变量,而不是从<code>GeneratorFunction</code>构造函数调用的范围的变量。这与使用{{jsxref("Global_Objects/eval", "eval")}}与生成函数表达式的代码不同。</p> +</div> + +<p>将<code>GeneratorFunction</code>构造函数调用为函数(不使用<code>new</code>运算符)与将其作为构造函数调用的效果相同。</p> + +<h2 id="Properties" name="Properties">属性</h2> + +<dl> + <dt><code><strong>GeneratorFunction.length</strong></code></dt> + <dd><code>GeneratorFunction</code>构造函数的 length 属性值为 1。</dd> + <dt>{{jsxref("GeneratorFunction.prototype")}}</dt> + <dd>允许向所有生成器函数对象添加属性。</dd> +</dl> + +<h2 id="GeneratorFunction_原型对象"><code>GeneratorFunction</code> 原型对象</h2> + +<h3 id="属性">属性</h3> + +<div>{{page('/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/GeneratorFunction/prototype', 'Properties')}}</div> + +<h2 id="GeneratorFunction_instances" name="GeneratorFunction_instances"><code>GeneratorFunction</code> 实例</h2> + +<p><code>GeneratorFunction</code>实例从{{jsxref("GeneratorFunction.prototype")}}继承方法和属性。与所有构造函数一样,你可以更改构造函数的原型对象以对所有<code>GeneratorFunction</code>实例进行更改。</p> + +<h2 id="Examples" name="Examples">示例</h2> + +<h3 id="从GeneratorFunction构造函数创建一个生成器函数">从<code>GeneratorFunction</code>构造函数创建一个生成器函数</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="规范">规范</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="浏览器兼容">浏览器兼容</h2> + +<div class="hidden">The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</div> + +<p>{{Compat("javascript.builtins.GeneratorFunction")}}</p> + +<h2 id="See_also" name="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> diff --git a/files/zh-cn/web/javascript/reference/global_objects/generatorfunction/prototype/index.html b/files/zh-cn/web/javascript/reference/global_objects/generatorfunction/prototype/index.html new file mode 100644 index 0000000000..c723725c05 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/generatorfunction/prototype/index.html @@ -0,0 +1,64 @@ +--- +title: GeneratorFunction.prototype +slug: Web/JavaScript/Reference/Global_Objects/GeneratorFunction/prototype +tags: + - ECMAScript 2015 + - GeneratorFunction + - Iterator + - JavaScript + - Property + - Prototype + - Reference +translation_of: Web/JavaScript/Reference/Global_Objects/GeneratorFunction +--- +<div>{{JSRef}}</div> + +<p><code><strong>GeneratorFunction.prototype</strong></code>属性是{{jsxref("GeneratorFunction")}}的原型对象。</p> + +<h2 id="Description" name="Description">描述</h2> + +<p>{{jsxref("GeneratorFunction")}} 的实例对象都继承于 <code>GeneratorFunction.prototype</code>. <code>GeneratorFunction.prototype</code> 不能被修改。</p> + +<h2 id="Properties" name="Properties">属性</h2> + +<dl> + <dt><code><strong>GeneratorFunction.constructor</strong></code></dt> + <dd>初始值是 {{jsxref("GeneratorFunction")}}.</dd> + <dt><code><strong>GeneratorFunction.prototype.prototype</strong></code></dt> + <dd>值是 <code>%GeneratorPrototype%</code>.</dd> +</dl> + +<h2 id="规范">规范</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.prototype', 'GeneratorFunction.prototype')}}</td> + <td>{{Spec2('ES2015')}}</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-generatorfunction.prototype', 'GeneratorFunction.prototype')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容">浏览器兼容</h2> + +<div class="hidden">The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</div> + +<p>{{Compat("javascript.builtins.GeneratorFunction.prototype")}}</p> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li>{{jsxref("GeneratorFunction")}}</li> + <li>{{jsxref("Function")}}</li> +</ul> |