diff options
Diffstat (limited to 'files/zh-cn/web/javascript/reference/operators')
3 files changed, 0 insertions, 426 deletions
diff --git a/files/zh-cn/web/javascript/reference/operators/array_comprehensions/index.html b/files/zh-cn/web/javascript/reference/operators/array_comprehensions/index.html deleted file mode 100644 index 8bdfa28db2..0000000000 --- a/files/zh-cn/web/javascript/reference/operators/array_comprehensions/index.html +++ /dev/null @@ -1,157 +0,0 @@ ---- -title: 数组推导式 -slug: Web/JavaScript/Reference/Operators/Array_comprehensions -tags: - - JavaScript - - Non-standard - - 参考 - - 运算符 -translation_of: Archive/Web/JavaScript/Array_comprehensions ---- -<div class="warning"> -<p><strong>非标准。不要使用!</strong><br> - 数组推导是非标准的。以后应该用 {{jsxref("Array.prototype.map")}},{{jsxref("Array.prototype.filter")}},{{jsxref("Functions/Arrow_functions", "箭头函数", "", 1)}}和{{jsxref("Operators/Spread_operator", "展开语法", "", 1)}}.。</p> -</div> - -<p>{{jsSidebar("Operators")}} </p> - -<p><strong>数组推导式</strong>是一种 JavaScript 表达式语法,使用它,你可以在一个原有数组的基础上快速的构造出一个新的数组。但是它已经从标准和火狐中移除。不要用它!</p> - -<h2 id="语法">语法</h2> - -<pre class="syntaxbox">[for (x of iterable) x] -[for (x of iterable) if (condition) x] -[for (x of iterable) for (y of iterable) x + y] -</pre> - -<h2 id="描述">描述</h2> - -<p>在数组推导式内部,可以使用下面两种子语句:</p> - -<ul> - <li><a href="/zh-CN/docs/Web/JavaScript/Reference/Statements/for...of">for...of</a></li> - <li><a href="/zh-CN/docs/Web/JavaScript/Reference/Statements/if...else">if</a></li> -</ul> - -<p>每个 <code>for-of</code> 语句都放在与其配对的 <code>if</code> 语句(可以有多个,也可以完全省略)的左边,每个数组推导式中可以包含多组这样的配对,但最终选取的表达式值只能有一个,且这个值(也可以是个数组推导式,也就是说可以嵌套)只能放在推导式的最右边,紧靠着右中括号。</p> - -<h2 id="示例">示例</h2> - -<h3 id="基本的数组推导式写法">基本的数组推导式写法</h3> - -<pre class="brush:js">[for (i of [ 1, 2, 3 ]) i*i ]; -// [ 1, 4, 9 ] - -var abc = [ "A", "B", "C" ]; -[for (letters of abc) letters.toLowerCase()]; -// [ "a", "b", "c" ]</pre> - -<h3 id="带有_if_语句的数组推导式">带有 if 语句的数组推导式</h3> - -<pre class="brush: js">var years = [ 1954, 1974, 1990, 2006, 2010, 2014 ]; - -[for (year of years) if (year > 2000) year]; -// [ 2006, 2010, 2014 ] - -[for (year of years) if (year > 2000) if(year < 2010) year]; -// [ 2006], 和下面的写法等效: - -[for (year of years) if (year > 2000 && year < 2010) year]; -// [ 2006] -</pre> - -<h3 id="用数组推导式比用数组的_map、filter_方法更简洁">用数组推导式比用数组的 <code>map</code>、<code>filter</code> 方法更简洁</h3> - -<p>对比数组的 {{jsxref("Array.map", "map")}} 和 {{jsxref("Array.filter", "filter")}} 方法:</p> - -<pre class="brush: js">var numbers = [ 1, 2, 3 ]; - -numbers.map(function (i) { return i * i }); -[for (i of numbers) i*i ]; -// 返回值都是 [ 1, 4, 9 ] - -numbers.filter(function (i) { return i < 3 }); -[for (i of numbers) if (i < 3) i]; -// 返回值都是 [ 1, 2 ] -</pre> - -<h3 id="带有两个数组的数组推导式">带有两个数组的数组推导式</h3> - -<p>用两个 <code>for-of</code> 语句迭代两个不同的数组:</p> - -<pre class="brush: js">var numbers = [ 1, 2, 3 ]; -var letters = [ "a", "b", "c" ]; - -var cross = [for (i of numbers) for (j of letters) i+j]; -// [ "1a", "1b", "1c", "2a", "2b", "2c", "3a", "3b", "3c" ] - -var grid = [for (i of numbers) [for (j of letters) i+j]]; -// [ -// ["1a", "1b", "1c"], -// ["2a", "2b", "2c"], -// ["3a", "3b", "3c"] -// ] - -[for (i of numbers) if (i > 1) for (j of letters) if(j > "a") i+j] -// ["2b", "2c", "3b", "3c"],和下面的写法<strong>等效</strong>: - -[for (i of numbers) for (j of letters) if (i > 1) if(j > "a") i+j] -// ["2b", "2c", "3b", "3c"] - -[for (i of numbers) if (i > 1) [for (j of letters) if(j > "a") i+j]] -// [["2b", "2c"], ["3b", "3c"]],和下面的写法<strong>不等效</strong>: - -[for (i of numbers) [for (j of letters) if (i > 1) if(j > "a") i+j]] -// [[], ["2b", "2c"], ["3b", "3c"]] -</pre> - -<h2 id="规范">规范</h2> - -<p>最初起草在ECMAScript 6草案中,但在第27版(2014年8月)中被移除。 请参阅ES 6的旧修订版的规范语义。</p> - -<h2 id="浏览器兼容性">浏览器兼容性</h2> - -<div class="hidden"><span style='background-color: transparent; color: #333333; display: inline !important; float: none; font-family: "Open Sans",arial,x-locale-body,sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; white-space: normal;'>本页的浏览器兼容性表都是基于结构化数据,如果你想更新数据.可以查看</span> <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> <span style='background-color: transparent; color: #333333; display: inline !important; float: none; font-family: "Open Sans",arial,x-locale-body,sans-serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; white-space: normal;'>并且请给我们发送合并请求</span>.</div> - -<p>{{Compat("javascript.operators.array_comprehensions")}}</p> - -<h2 id="同旧版的JS1.7JS1.8数组推导的不同之处">同旧版的JS1.7/JS1.8数组推导的不同之处</h2> - -<p> </p> - -<div class="warning">JS1.7/JS1.8数组推导 在Gecko的46版本中已经被移除了 ({{bug(1220564)}}).</div> - -<p><strong>旧版数组推导语法 (请不要再使用了!):</strong></p> - -<pre class="brush: js example-bad">[X for (Y in Z)] -[X for each (Y in Z)] -[X for (Y of Z)] -</pre> - -<p>不同点:</p> - -<ul> - <li>ESNext数组推导为每个"for"创建了一个作用域而取代了整个作用域. - <ul> - <li>Old: <code>[()=>x for (x of [0, 1, 2])][1]() // 2</code></li> - <li>New: <code>[for (x of [0, 1, 2]) ()=>x][1]() // 1, each iteration creates a fresh binding for x. </code></li> - </ul> - </li> - <li>ESNext 同"for"进行赋值而取代了旧的赋值表达式. - <ul> - <li>Old: <code>[i * 2 for (i of numbers)]</code></li> - <li>New: <code>[for (i of numbers) i * 2]</code></li> - </ul> - </li> - <li>ESNext数组推导可由多个if和for组成</li> - <li>ESNext数组推导只和<code>{{jsxref("Statements/for...of", "for...of")}}</code>迭代才有效,而不会同 <code>{{jsxref("Statements/for...in", "for...in")}}</code> 迭代.</li> -</ul> - -<p>点击查看 <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=1220564#c42">Bug 1220564, comment 42</a> 并提出建设性建议.</p> - -<h2 id="See_also" name="See_also">相关链接</h2> - -<ul> - <li><a href="/zh-CN/docs/Web/JavaScript/Reference/Statements/for...of"><code>for...of</code></a></li> - <li>{{jsxref("Operators/Generator_comprehensions", "生成器推导式", "" ,1)}}</li> -</ul> diff --git a/files/zh-cn/web/javascript/reference/operators/expression_closures/index.html b/files/zh-cn/web/javascript/reference/operators/expression_closures/index.html deleted file mode 100644 index e5dee577bc..0000000000 --- a/files/zh-cn/web/javascript/reference/operators/expression_closures/index.html +++ /dev/null @@ -1,76 +0,0 @@ ---- -title: Expression closures -slug: Web/JavaScript/Reference/Operators/Expression_closures -tags: - - Functions - - JavaScript - - Reference -translation_of: Archive/Web/JavaScript/Expression_closures ---- -<div class="warning"><strong>非标准,不要使用!</strong><br> -闭包表达式语法是废弃的 SpiderMonkey 的特性,并且<a href="https://bugzilla.mozilla.org/show_bug.cgi?id=1083458">将被移除</a>。为了长远使用,考虑使用<a href="/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions">箭头函数</a>。</div> - -<div>{{jsSidebar("Operators")}}</div> - -<p>表达式闭包是定义简单函数的一种便捷方式。</p> - -<h2 id="语法">语法</h2> - -<pre class="syntaxbox">function [<em>name</em>]([<em>param1</em>[, <em>param2[</em>, ..., <em>paramN</em>]]]) - <em>expression</em> -</pre> - -<h3 id="参数">参数</h3> - -<dl> - <dt><code>name</code></dt> - <dd>函数名。函数名可以省略不写,称为匿名函数。函数名仅在函数体有效。</dd> - <dt><code>paramN</code></dt> - <dd>形参名。一个函数最多可以有255个参数。</dd> - <dt><code>expression</code></dt> - <dd>构成函数体的表达式。</dd> -</dl> - -<h2 id="描述">描述</h2> - -<p>这一附加特性只是编写简单函数的快捷方式,让语言更类似通常的 <a class="external" href="http://en.wikipedia.org/wiki/Lambda_calculus#Lambda_calculus_and_programming_languages">Lambda 标记</a>。</p> - -<p>JavaScript 1.7 及之前版本:</p> - -<pre class="brush: js">function(x) { return x * x; }</pre> - -<p>JavaScript 1.8:</p> - -<pre class="brush: js">function(x) x * x</pre> - -<p>该语法支持省略花括号和'return'语句。使用这种编码的目的只是为了在句法上使得代码更加简化,但除此之外没有其他好处。</p> - -<h2 id="示例">示例</h2> - -<p>一种绑定事件监听器的便捷方式:</p> - -<pre class="brush: js"> document.addEventListener("click", function() false, true); -</pre> - -<p>在 JavaScript 1.6 中的一些数组函数中使用该标记:</p> - -<pre class="brush: js">elems.some(function(elem) elem.type == "text"); -</pre> - -<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.operators.expression_closures")}}</p> - -<h2 id="相关链接">相关链接</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> diff --git a/files/zh-cn/web/javascript/reference/operators/generator_comprehensions/index.html b/files/zh-cn/web/javascript/reference/operators/generator_comprehensions/index.html deleted file mode 100644 index 1442d50019..0000000000 --- a/files/zh-cn/web/javascript/reference/operators/generator_comprehensions/index.html +++ /dev/null @@ -1,193 +0,0 @@ ---- -title: Generator推导式 -slug: Web/JavaScript/Reference/Operators/Generator_comprehensions -translation_of: Archive/Web/JavaScript/Generator_comprehensions ---- -<div class="warning"><strong>非标准的。不要使用!</strong><br> -generator推导式是非标准的,而且它不太可能会被添加到ECMAScript。考虑到将来,请使用 {{jsxref("Statements/function*", "generator", "", 1)}}。 -<p> </p> -</div> - -<p>{{jsSidebar("Operators")}}</p> - -<p>生成器推导语法是一种JavaScript表达式,它允许您基于现有的可迭代对象快速组合新的生成器函数。</p> - -<p>许多编程语言中都存在推导。</p> - -<p>看下面,原来Generator推导式语法在SpiderMonkey的不同之处,它是基于对ECMAScript4的提议。</p> - -<h2 id="语法">语法</h2> - -<pre class="syntaxbox">(for (x of iterable) x) -(for (x of iterable) if (condition) x) -(for (x of iterable) for (y of iterable) x + y) -</pre> - -<h2 id="描述">描述</h2> - -<p>在Generator推导式中,这两种构成方式都是允许的:</p> - -<ul> - <li>{{jsxref("Statements/for...of", "for...of")}} </li> - <li>{{jsxref("Statements/if...else", "if")}}</li> -</ul> - -<p>for-of迭代器是构成的第一个部分。当由多重部分构成时,后面for-of和if构成方式都是被允许的。</p> - -<h2 id="示例">示例</h2> - -<h3 id="单个构成部分的_generator推导式:">单个构成部分的 generator推导式:</h3> - -<pre class="brush:js">(for (i of [ 1, 2, 3 ]) i*i ); -// generator function which yields 1, 4, and 9 - -[...(for (i of [ 1, 2, 3 ]) i*i )]; -// [1, 4, 9] - -var abc = [ "A", "B", "C" ]; -(for (letters of abc) letters.toLowerCase()); -// generator function which yields "a", "b", and "c" - -</pre> - -<h3 id="有if伴随的多重构成的gennerator推导式:">有if伴随的多重构成的gennerator推导式:</h3> - -<pre class="brush: js">var years = [ 1954, 1974, 1990, 2006, 2010, 2014 ]; - -(for (year of years) if (year > 2000) year); -// generator function which yields 2006, 2010, and 2014 - -(for (year of years) if (year > 2000) if(year < 2010) year); -// generator function which yields 2006, the same as below: - -(for (year of years) if (year > 2000 && year < 2010) year); -// generator function which yields 2006 -</pre> - -<h3 id="Generator推导式与Generator函数对比">Generator推导式与Generator函数对比</h3> - -<p>用一种简单的方式来理解generator推导式的语法并与generator函数来做个比较。</p> - -<p>Example 1: 仅是 generator.</p> - -<pre class="brush: js">var numbers = [ 1, 2, 3 ]; - -// Generator 函数 -(function*() { - for (let i of numbers) { - yield i * i; - } -})() - -// Generator 推导式 -(for (i of numbers) i*i ); - -// 结果: 两者都得到 yields [ 1, 4, 9 ] -</pre> - -<p>Example 2: 在 generator 中用if.</p> - -<pre class="brush: js">var numbers = [ 1, 2, 3 ]; - -// Generator 函数 -(function*() { - for (let i of numbers) { - if (i < 3) { - yield i * 1; - } - } -})() - -// Generator 推导式 -(for (i of numbers) if (i < 3) i); - -// 结果: 两者都得到 yields [ 1, 2 ]</pre> - -<h2 id="规范">规范</h2> - -<p>Generator推导式是最初在ECMAScript 2015中进行拟稿,但是在14年8月27号修订中被移除了。 请参阅较旧版本的ES2015规范语义.</p> - -<h2 id="浏览器兼容性">浏览器兼容性</h2> - -<p>{{CompatibilityTable}}</p> - -<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>{{CompatNo}}</td> - <td>{{ CompatGeckoDesktop("30") }}</td> - <td>{{CompatNo}}</td> - <td>{{CompatNo}}</td> - <td>{{CompatNo}}</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>{{CompatNo}}</td> - <td>{{CompatNo}}</td> - <td>{{ CompatGeckoMobile("30") }}</td> - <td>{{CompatNo}}</td> - <td>{{CompatNo}}</td> - <td>{{CompatNo}}</td> - </tr> - </tbody> -</table> -</div> - -<h3 id="SpiderMonkey的具体实现笔记">SpiderMonkey的具体实现笔记</h3> - -<ul> - <li>{{jsxref("Statements/let", "let")}} 作为标识符,因为let当前仅可用于JS版本1.7和XUL脚本标记.</li> - <li>目前还不支持解构 ({{bug(980828)}}).</li> -</ul> - -<h3 id="与旧的JS1.7_JS1.8理解的区别">与旧的JS1.7 / JS1.8理解的区别</h3> - -<ul> - <li>ES2016 的解析为每个“for”节点创建一个范围,而不是作为一个整体的理解。 - <ul> - <li>Old: <code>[...(()=>x for (x of [0, 1, 2]))][1]() // 2</code></li> - <li>New: <code>[...(for (x of [0, 1, 2]) ()=>x)][1]() // 1, 每个迭代都会创建一个新的x的绑定事件。</code></li> - </ul> - </li> - <li> ES2016的解析以“for”而不是赋值表达式开头。 - <ul> - <li>Old: <code>(i * 2 for (i of numbers))</code></li> - <li>New: <code>(for (i of numbers) <code>i * 2</code>)</code></li> - </ul> - </li> - <li>ES2016 解析可以有多个if和for组件。</li> - <li>ES2016 解析仅这种方式工作<code>{{jsxref("Statements/for...of", "for...of")}}</code> 而不是<code>{{jsxref("Statements/for...in", "for...in")}}</code> 的方式迭代。</li> -</ul> - -<h2 id="相关链接">相关链接</h2> - -<ul> - <li>{{jsxref("Statements/for...of", "for...of")}}</li> - <li>{{jsxref("Operators/Array_comprehensions", "Array comprehensions")}}</li> -</ul> |