mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-12-16 07:51:52 +08:00
语法高亮,滚动条美化,设置页面调整
This commit is contained in:
@@ -4,8 +4,8 @@
|
||||
<div><iframe class="interactive interactive-js" frameborder="0" height="250" src="https://interactive-examples.mdn.mozilla.net/pages/js/array-from.html" width="100%"></iframe></div>
|
||||
<p class="hidden">The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone <a class="external" href="https://github.com/mdn/interactive-examples" rel="noopener">https://github.com/mdn/interactive-examples</a> and send us a pull request.</p>
|
||||
<h2 id="Syntax" name="Syntax">语法</h2>
|
||||
<pre class="syntaxbox"><code>Array.from(arrayLike[, mapFn[, thisArg]])
|
||||
</code></pre>
|
||||
<pre><code class="language-javascript"><code>Array.from(arrayLike[, mapFn[, thisArg]])
|
||||
</code></code></pre>
|
||||
<h3 id="Parameters" name="Parameters">参数</h3>
|
||||
<dl>
|
||||
<dt><code>arrayLike</code></dt>
|
||||
@@ -28,28 +28,28 @@
|
||||
<p>在 ES2015 中, <code>Class</code> 语法允许我们为内置类型(比如 <code>Array</code>)和自定义类新建子类(比如叫 <code>SubArray</code>)。这些子类也会继承父类的静态方法,比如 <code>SubArray.from()</code>,调用该方法后会返回子类 <code>SubArray</code> 的一个实例,而不是 <code>Array</code> 的实例。</p>
|
||||
<h2 id="示例">示例</h2>
|
||||
<h3 id="Array_from_a_String">Array from a <code>String</code></h3>
|
||||
<pre class="brush: js">Array.from('foo');
|
||||
// ["f", "o", "o"]</pre>
|
||||
<pre><code class="language-javascript">Array.from('foo');
|
||||
// ["f", "o", "o"]</code></pre>
|
||||
<h3 id="Array_from_a_Set">Array from a <code>Set</code></h3>
|
||||
<pre class="brush: js">let s = new Set(['foo', window]);
|
||||
<pre><code class="language-javascript">let s = new Set(['foo', window]);
|
||||
Array.from(s);
|
||||
// ["foo", window]
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h3 id="Array_from_a_Map">Array from a <code>Map</code></h3>
|
||||
<pre class="brush: js">let m = new Map([[1, 2], [2, 4], [4, 8]]);
|
||||
<pre><code class="language-javascript">let m = new Map([[1, 2], [2, 4], [4, 8]]);
|
||||
Array.from(m);
|
||||
// [[1, 2], [2, 4], [4, 8]]
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h3 id="Array_from_an_Array-like_object_(arguments)">Array from an Array-like object (arguments)</h3>
|
||||
<pre class="brush: js">function f() {
|
||||
<pre><code class="language-javascript">function f() {
|
||||
return Array.from(arguments);
|
||||
}
|
||||
|
||||
f(1, 2, 3);
|
||||
|
||||
// [1, 2, 3]</pre>
|
||||
// [1, 2, 3]</code></pre>
|
||||
<h3 id="在Array.from中使用箭头函数">在<code>Array.from</code>中使用箭头函数</h3>
|
||||
<pre class="brush: js">// Using an arrow function as the map function to
|
||||
<pre><code class="language-javascript">// Using an arrow function as the map function to
|
||||
// manipulate the elements
|
||||
Array.from([1, 2, 3], x => x + x);
|
||||
// x => x + x代表这是一个函数,只是省略了其他的定义,这是一种Lambda表达式的写法
|
||||
@@ -62,18 +62,18 @@ Array.from([1, 2, 3], x => x + x);
|
||||
// the value of `v` below will be `undefined`
|
||||
Array.from({length: 5}, (v, i) => i);
|
||||
// [0, 1, 2, 3, 4]
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h3 id="数组去重合并">数组去重合并</h3>
|
||||
<pre class="brush: js">function combine(){
|
||||
<pre><code class="language-javascript">function combine(){
|
||||
let arr = [].concat.apply([], arguments); //没有去重复的新数组
|
||||
return Array.from(new Set(arr));
|
||||
}
|
||||
|
||||
var m = [1, 2, 2], n = [2,3,3];
|
||||
console.log(combine(m,n)); // [1, 2, 3]</pre>
|
||||
console.log(combine(m,n)); // [1, 2, 3]</code></pre>
|
||||
<h2 id="Polyfill">Polyfill</h2>
|
||||
<p>ECMA-262 第六版标准添加了<code> Array.from </code>。有些实现中可能尚未包括在其中。你可以通过在脚本前添加如下内容作为替代方法,以使用未原生支持的 <code>Array.from</code> 方法。该算法按照 ECMA-262 第六版中的规范实现,并假定 <code>Object</code> 和 <code>TypeError</code> 有其本身的值, <code>callback.call</code> 对应 <a href="Reference/Global_Objects/Function/call" title="call() 方法调用一个函数, 其具有一个指定的this值和分别地提供的参数(参数的列表)。"><code>Function.prototype.call</code></a> 。此外,鉴于无法使用 Polyfill 实现真正的的迭代器,该实现不支持规范中定义的泛型可迭代元素。</p>
|
||||
<pre class="brush: js">// Production steps of ECMA-262, Edition 6, 22.1.2.1
|
||||
<pre><code class="language-javascript">// Production steps of ECMA-262, Edition 6, 22.1.2.1
|
||||
// Reference: https://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.from
|
||||
if (!Array.from) {
|
||||
Array.from = (function () {
|
||||
@@ -165,7 +165,7 @@ if (!Array.from) {
|
||||
};
|
||||
}());
|
||||
}
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h2 id="规范">规范</h2>
|
||||
<table class="standard-table">
|
||||
<tbody>
|
||||
|
||||
Reference in New Issue
Block a user