mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-08 23:14:06 +08:00
319 lines
42 KiB
HTML
319 lines
42 KiB
HTML
<article id="wikiArticle">
|
||
<div></div>
|
||
<p>The <strong><code>Uint16Array</code></strong> typed array represents an array of 16-bit unsigned integers in the platform byte order. If control over byte order is needed, use <a href="Reference/Global_Objects/DataView" title="DataView 视图是一个可以从 ArrayBuffer 对象中读写多种数值类型的底层接口,使用它时,不用考虑不同平台的字节序问题。"><code>DataView</code></a> instead. The contents are initialized to <code>0</code>. Once established, you can reference elements in the array using the object's methods, or using standard array index syntax (that is, using bracket notation).</p>
|
||
<h2 id="语法">语法</h2>
|
||
<pre><code class="language-javascript">new Uint16Array(); // ES2017 新加入的
|
||
new Uint16Array(length);
|
||
new Uint16Array(typedArray);
|
||
new Uint16Array(object);
|
||
new Uint16Array(buffer [, byteOffset [, length]]);
|
||
</code></pre>
|
||
<p>获取有关构造函数语法和参数的更多信息,访问<em><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray#Syntax">TypedArray</a>。</em></p>
|
||
<h2 id="属性">属性</h2>
|
||
<dl>
|
||
<dt><a href="Reference/Global_Objects/TypedArray/BYTES_PER_ELEMENT" title="TypedArray.BYTES_PER_ELEMENT 属性代表了强类型数组中每个元素所占用的字节数。"><code>Uint16Array.BYTES_PER_ELEMENT</code></a></dt>
|
||
<dd>返回元素大小的数值. <code>2</code> in the case of an <code>Uint16Array</code>.</dd>
|
||
<dt>Uint16Array.length</dt>
|
||
<dd>Static length property whose value is 0. For the actual length (number of elements), see <a href="Reference/Global_Objects/TypedArray/length" title="length访问器属性表示类型化数组的长度(元素数)。"><code>Uint16Array.prototype.length</code></a>.</dd>
|
||
<dt><a href="Reference/Global_Objects/TypedArray/name" title="TypedArray.name ?属性是描述类型数组构造名的字符串值。"><code>Uint16Array.name</code></a></dt>
|
||
<dd>Returns the string value of the constructor name. In the case of the <code>Uint16Array</code> type: "Uint16Array".</dd>
|
||
<dt><a href="Reference/Global_Objects/TypedArray/prototype" title="TypedArray.prototype属性表示TypedArray构造器的原型."><code>Uint16Array.prototype</code></a></dt>
|
||
<dd>Prototype for the <em>TypedArray</em> objects.</dd>
|
||
</dl>
|
||
<h2 id="Methods">Methods</h2>
|
||
<dl>
|
||
<dt><a href="Reference/Global_Objects/TypedArray/from" title="TypedArray.from() 方法 从一个类数组或者可迭代对象中创建一个新类型数组。 这个方法和 Array.from()类似。"><code>Uint16Array.from()</code></a></dt>
|
||
<dd>Creates a new <code>Uint16Array</code> from an array-like or iterable object. See also <a href="Reference/Global_Objects/Array/from" title="Array.from() 方法从一个类似数组或可迭代对象中创建一个新的数组实例。"><code>Array.from()</code></a>.</dd>
|
||
<dt><a href="Reference/Global_Objects/TypedArray/of" title="TypedArray.of() 方法创建一个具有可变数量参数的新类型数组 。此方法几乎与Array.of() 相同。"><code>Uint16Array.of()</code></a></dt>
|
||
<dd>Creates a new <code>Uint16Array</code> with a variable number of arguments. See also <a href="Reference/Global_Objects/Array/of" title="Array.of() 方法创建一个具有可变数量参数的新数组实例,而不考虑参数的数量或类型。"><code>Array.of()</code></a>.</dd>
|
||
</dl>
|
||
<h2 id="Uint16Array_prototype"><code>Uint16Array</code> prototype</h2>
|
||
<p>All <code>Uint16Array</code> objects inherit from <a href="Reference/Global_Objects/TypedArray/prototype" title="TypedArray.prototype属性表示TypedArray构造器的原型."><code>%TypedArray%.prototype</code></a>.</p>
|
||
<h3 id="Properties">Properties</h3>
|
||
<dl>
|
||
<dt><code>Uint16Array.prototype.constructor</code></dt>
|
||
<dd>Returns the function that created an instance's prototype. This is the <code>Uint16Array</code> constructor by default.</dd>
|
||
<dt><a href="Reference/Global_Objects/TypedArray/buffer" title="buffer访问器属性表示由TypedArray在构造期间引用的ArrayBuffer。"><code>Uint16Array.prototype.buffer</code></a> <span class="inlineIndicator readOnly readOnlyInline" title="该属性的值无法更改">只读 </span></dt>
|
||
<dd>Returns the <a href="Reference/Global_Objects/ArrayBuffer" title="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 https://github.com/mdn/interactive-examples and send us a pull request."><code>ArrayBuffer</code></a> referenced by the <code>Uint16Array</code> Fixed at construction time and thus <strong>read only</strong>.</dd>
|
||
<dt><a href="Reference/Global_Objects/TypedArray/byteLength" title="byteLength访问器属性表示类型化数组的长度(字节数)。"><code>Uint16Array.prototype.byteLength</code></a> <span class="inlineIndicator readOnly readOnlyInline" title="该属性的值无法更改">只读 </span></dt>
|
||
<dd>Returns the length (in bytes) of the <code>Uint16Array</code> from the start of its <a href="Reference/Global_Objects/ArrayBuffer" title="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 https://github.com/mdn/interactive-examples and send us a pull request."><code>ArrayBuffer</code></a>. Fixed at construction time and thus <strong>read only.</strong></dd>
|
||
<dt><a href="Reference/Global_Objects/TypedArray/byteOffset" title="byteOffset 访问器属性表示类型化数组距离其ArrayBuffer起始位置的偏移(字节数)。"><code>Uint16Array.prototype.byteOffset</code></a> <span class="inlineIndicator readOnly readOnlyInline" title="该属性的值无法更改">只读 </span></dt>
|
||
<dd>Returns the offset (in bytes) of the <code>Uint16Array</code> from the start of its <a href="Reference/Global_Objects/ArrayBuffer" title="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 https://github.com/mdn/interactive-examples and send us a pull request."><code>ArrayBuffer</code></a>. Fixed at construction time and thus <strong>read only.</strong></dd>
|
||
<dt><a href="Reference/Global_Objects/TypedArray/length" title="length访问器属性表示类型化数组的长度(元素数)。"><code>Uint16Array.prototype.length</code></a> <span class="inlineIndicator readOnly readOnlyInline" title="该属性的值无法更改">只读 </span></dt>
|
||
<dd>Returns the number of elements hold in the <code>Uint16Array</code>. Fixed at construction time and thus <strong>read only.</strong></dd>
|
||
</dl>
|
||
<h3 id="Methods_2">Methods</h3>
|
||
<dl>
|
||
<dt><a href="Reference/Global_Objects/TypedArray/copyWithin" title="copyWithin()方法将数组中的元素序列复制到以target起始的位置。 副本取自第二个参数和第三个参数 start 和end的位置下标。end 参数是可选的,默认为数组长度。这个方法的算法和Array.prototype.copyWithin相同。 TypedArray 是这里的 类型化数组类型 之一。"><code>Uint16Array.prototype.copyWithin()</code></a></dt>
|
||
<dd>Copies a sequence of array elements within the array. See also <a href="Reference/Global_Objects/Array/copyWithin" title="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 https://github.com/mdn/interactive-examples and send us a pull request."><code>Array.prototype.copyWithin()</code></a>.</dd>
|
||
<dt><a href="Reference/Global_Objects/TypedArray/entries" title="The entries()返回新的Array Iterator对象,包含数组每个下标处的键值对。"><code>Uint16Array.prototype.entries()</code></a></dt>
|
||
<dd>Returns a new <code>Array Iterator</code> object that contains the key/value pairs for each index in the array. See also <a href="Reference/Global_Objects/Array/entries" title="entries() 方法返回一个新的Array Iterator对象,该对象包含数组中每个索引的键/值对。"><code>Array.prototype.entries()</code></a>.</dd>
|
||
<dt><a href="Reference/Global_Objects/TypedArray/every" title="every() 方法测试类型化数组的所有元素是否都能够通过由提供函数实现的测试。这个方法的算法与 Array.prototype.every()相同。 TypedArray 是这里的 类型化数组类型 之一。"><code>Uint16Array.prototype.every()</code></a></dt>
|
||
<dd>Tests whether all elements in the array pass the test provided by a function. See also <a href="Reference/Global_Objects/Array/every" title="every() 方法测试数组的所有元素是否都通过了指定函数的测试。"><code>Array.prototype.every()</code></a>.</dd>
|
||
<dt><a href="Reference/Global_Objects/TypedArray/fill" title="fill() 方法将类型化数组中的从起始索引到终止索引内的全部元素。这个方法的算法和 Array.prototype.fill() 相同。 TypedArray 是这里的类型化数组类型之一。"><code>Uint16Array.prototype.fill()</code></a></dt>
|
||
<dd>Fills all the elements of an array from a start index to an end index with a static value. See also <a href="Reference/Global_Objects/Array/fill" title="fill() 方法用一个固定值填充一个数组中从起始索引到终止索引内的全部元素。不包括终止索引。"><code>Array.prototype.fill()</code></a>.</dd>
|
||
<dt><a href="Reference/Global_Objects/TypedArray/filter" title="filter()创建新的类型化数组,含有所有通过了测试的元素,测试由提供的函数实现。这个方法的算法和 Array.prototype.filter()相同。 TypedArray 是这里的 类型化数组类型 之一。"><code>Uint16Array.prototype.filter()</code></a></dt>
|
||
<dd>Creates a new array with all of the elements of this array for which the provided filtering function returns true. See also <a href="Reference/Global_Objects/Array/filter" title="filter() 方法创建一个新数组, 其包含通过所提供函数实现的测试的所有元素。"><code>Array.prototype.filter()</code></a>.</dd>
|
||
<dt><a href="Reference/Global_Objects/TypedArray/find" title="如果某个元素满足所提供的测试函数,find()方法返回类型化数组中的 值。否则返回undefined 。 TypedArray 是这里的 类型化数组类型 之一。"><code>Uint16Array.prototype.find()</code></a></dt>
|
||
<dd>Returns the found value in the array, if an element in the array satisfies the provided testing function or <code>undefined</code> if not found. See also <a href="Reference/Global_Objects/Array/find" title="find() 方法返回数组中满足提供的测试函数的第一个元素的值。否则返回 undefined。"><code>Array.prototype.find()</code></a>.</dd>
|
||
<dt><a href="Reference/Global_Objects/TypedArray/findIndex" title="如果某个元素满足所提供的测试函数,findIndex()方法返回类型化数组中的 下标。否则返回 -1。 TypedArray 是这里的 类型化数组类型 之一。"><code>Uint16Array.prototype.findIndex()</code></a></dt>
|
||
<dd>Returns the found index in the array, if an element in the array satisfies the provided testing function or -1 if not found. See also <a href="Reference/Global_Objects/Array/findIndex" title="findIndex()方法返回数组中满足提供的测试函数的第一个元素的索引。否则返回-1。"><code>Array.prototype.findIndex()</code></a>.</dd>
|
||
<dt><a href="Reference/Global_Objects/TypedArray/forEach" title="forEach()方法对类型化数组的每个元素调用提供的函数。 这个方法的算法和 Array.prototype.forEach()相同。 TypedArray 是这里的 类型化数组类型 之一。"><code>Uint16Array.prototype.forEach()</code></a></dt>
|
||
<dd>Calls a function for each element in the array. See also <a href="Reference/Global_Objects/Array/forEach" title="forEach() 方法对数组的每个元素执行一次提供的函数。"><code>Array.prototype.forEach()</code></a>.</dd>
|
||
<dt><a href="Reference/Global_Objects/TypedArray/includes" title="includes()方法判断类型化数组中是否含有特定元素,并相应返回true 或者false ,这个方法的算法和Array.prototype.includes()相同。 TypedArray 是这里的 类型化数组 之一。"><code>Uint16Array.prototype.includes()</code></a> <span title="这是一个实验性的 API,请尽量不要在生产环境中使用它。"><i class="icon-beaker"> </i></span></dt>
|
||
<dd>Determines whether a typed array includes a certain element, returning <code>true</code> or <code>false</code> as appropriate. See also <a href="Reference/Global_Objects/Array/includes" title="includes() 方法用来判断一个数组是否包含一个指定的值,根据情况,如果包含则返回 true,否则返回false。"><code>Array.prototype.includes()</code></a>.</dd>
|
||
<dt><a href="Reference/Global_Objects/TypedArray/indexOf" title="indexOf() 方法返回在类型数组中可以找到给定元素的第一个索引,如果不存在,则返回-1。 方法具有与 Array.prototype.indexOf() 相同的算法。 TypedArray是这里的类型化数组类型之一。"><code>Uint16Array.prototype.indexOf()</code></a></dt>
|
||
<dd>Returns the first (least) index of an element within the array equal to the specified value, or -1 if none is found. See also <a href="Reference/Global_Objects/Array/indexOf" title="indexOf()方法返回在数组中可以找到一个给定元素的第一个索引,如果不存在,则返回-1。"><code>Array.prototype.indexOf()</code></a>.</dd>
|
||
<dt><a href="Reference/Global_Objects/TypedArray/join" title="join()方法将数组中所有元素连接为一个字符串。这个方法的算法和Array.prototype.join()相同。 TypedArray 是这里的 类型化数组 之一。"><code>Uint16Array.prototype.join()</code></a></dt>
|
||
<dd>Joins all elements of an array into a string. See also <a href="Reference/Global_Objects/Array/join" title="join() 方法将一个数组(或一个类数组对象)的所有元素连接成一个字符串并返回这个字符串。如果数组只有一个项目,那么将返回该项目而不使用分隔符。"><code>Array.prototype.join()</code></a>.</dd>
|
||
<dt><a href="Reference/Global_Objects/TypedArray/keys" title="keys()方法返回新的 Array Iterator 对象,包含数组中每个下标的键。"><code>Uint16Array.prototype.keys()</code></a></dt>
|
||
<dd>Returns a new <code>Array Iterator</code> that contains the keys for each index in the array. See also <a href="Reference/Global_Objects/Array/keys" title="keys() 方法返回一个包含数组中每个索引键的Array Iterator对象。"><code>Array.prototype.keys()</code></a>.</dd>
|
||
<dt><a href="Reference/Global_Objects/TypedArray/lastIndexOf" title="lastIndexOf() 方法返回在类型数组中可以找到给定元素的最后一个索引,如果不存在,则返回-1。 方法具有与 Array.prototype.lastIndexOf() 相同的算法。 TypedArray是这里的类型化数组类型之一。"><code>Uint16Array.prototype.lastIndexOf()</code></a></dt>
|
||
<dd>Returns the last (greatest) index of an element within the array equal to the specified value, or -1 if none is found. See also <a href="Reference/Global_Objects/Array/lastIndexOf" title="lastIndexOf() 方法返回指定元素(也即有效的 JavaScript 值或变量)在数组中的最后一个的索引,如果不存在则返回 -1。从数组的后面向前查找,从 fromIndex 处开始。"><code>Array.prototype.lastIndexOf()</code></a>.</dd>
|
||
<dt><a href="Reference/Global_Objects/TypedArray/map" title="map()方法对类型化数组的每个元素调用提供的函数,并使用结果来创建新的类型化数组。 这个方法的算法和 Array.prototype.map()相同。 TypedArray 是这里的 类型化数组类型 之一。"><code>Uint16Array.prototype.map()</code></a></dt>
|
||
<dd>Creates a new array with the results of calling a provided function on every element in this array. See also <a href="Reference/Global_Objects/Array/map" title="map() 方法创建一个新数组,其结果是该数组中的每个元素都调用一个提供的函数后返回的结果。"><code>Array.prototype.map()</code></a>.</dd>
|
||
<dt><a href="Reference/Global_Objects/TypedArray/move" title="move()方法将数组中的元素序列复制到以target起始的位置。但是,这个非标准方法已经被TypedArray.prototype.copyWithin() 标准方法取代。TypedArray 是这里的 类型化数组类型 之一。"><code>Uint16Array.prototype.move()</code></a> <span class="icon-only-inline" title="This API has not been standardized."><i class="icon-warning-sign"> </i></span> <span class="inlineIndicator unimplemented unimplementedInline">未实现</span></dt>
|
||
<dd>Former non-standard version of <a href="Reference/Global_Objects/TypedArray/copyWithin" title="copyWithin()方法将数组中的元素序列复制到以target起始的位置。 副本取自第二个参数和第三个参数 start 和end的位置下标。end 参数是可选的,默认为数组长度。这个方法的算法和Array.prototype.copyWithin相同。 TypedArray 是这里的 类型化数组类型 之一。"><code>Uint16Array.prototype.copyWithin()</code></a>.</dd>
|
||
<dt><a href="Reference/Global_Objects/TypedArray/reduce" title="reduce() 方法接受一个函数作为参数,这个函数作为一个累加器,从左到右遍历整个类型数组,最后返回一个单一的值. 这个方法和Array.prototype.reduce()使用了同样的算法. TypedArray 是一个 类型数组."><code>Uint16Array.prototype.reduce()</code></a></dt>
|
||
<dd>Apply a function against an accumulator and each value of the array (from left-to-right) as to reduce it to a single value. See also <a href="Reference/Global_Objects/Array/reduce" title="reduce() 方法对数组中的每个元素执行一个由您提供的reducer函数(升序执行),将其结果汇总为单个返回值。"><code>Array.prototype.reduce()</code></a>.</dd>
|
||
<dt><a href="Reference/Global_Objects/TypedArray/reduceRight" title="reduceRight()在累加器和类型化数组的每个元素上(从右到左)调用函数,使其归约为单一的值。这个方法的算法和 Array.prototype.reduceRight()相同。 TypedArray 是这里的类型化数组类型 之一。"><code>Uint16Array.prototype.reduceRight()</code></a></dt>
|
||
<dd>Apply a function against an accumulator and each value of the array (from right-to-left) as to reduce it to a single value. See also <a href="Reference/Global_Objects/Array/reduceRight" title="reduceRight() 方法接受一个函数作为累加器(accumulator)和数组的每个值(从右到左)将其减少为单个值。"><code>Array.prototype.reduceRight()</code></a>.</dd>
|
||
<dt><a href="Reference/Global_Objects/TypedArray/reverse" title="reverse()方法原地翻转类型化数组。类型化数组的第一个元素变为最后一个,最后一个变为第一个。这个方法的算法和Array.prototype.reverse()相同。 TypedArray 是这里的 类型化数组类型 之一。"><code>Uint16Array.prototype.reverse()</code></a></dt>
|
||
<dd>Reverses the order of the elements of an array — the first becomes the last, and the last becomes the first. See also <a href="Reference/Global_Objects/Array/reverse" title="reverse() 方法将数组中元素的位置颠倒,并返回该数组。该方法会改变原数组。"><code>Array.prototype.reverse()</code></a>.</dd>
|
||
<dt><a href="Reference/Global_Objects/TypedArray/set" title="set() 方法用于从指定数组中读取值,并将其存储在类型化数组中。"><code>Uint16Array.prototype.set()</code></a></dt>
|
||
<dd>Stores multiple values in the typed array, reading input values from a specified array.</dd>
|
||
<dt><a href="Reference/Global_Objects/TypedArray/slice" title="slice()方法返回一个typed array的部分类型数组对象,数组内容采用浅拷贝方式. 方法采用与 Array.prototype.slice()相同的算法. 类型数组是 typed array types成员中的一员 ."><code>Uint16Array.prototype.slice()</code></a></dt>
|
||
<dd>Extracts a section of an array and returns a new array. See also <a href="Reference/Global_Objects/Array/slice" title="The source for this interactive demo is stored in a GitHub repository. If you'd like to contribute to the interactive demo project, please clone https://github.com/mdn/interactive-examples and send us a pull request."><code>Array.prototype.slice()</code></a>.</dd>
|
||
<dt><a href="Reference/Global_Objects/TypedArray/some" title="这个 some() 方法检测 TypedArray 的一些元素是否通过所提供函数的测试. 这个方法和 Array.prototype.some() 相同. TypedArray 是 typed array types 之一."><code>Uint16Array.prototype.some()</code></a></dt>
|
||
<dd>Returns true if at least one element in this array satisfies the provided testing function. See also <a href="Reference/Global_Objects/Array/some" title="some() 方法测试是否至少有一个元素通过由提供的函数实现的测试。"><code>Array.prototype.some()</code></a>.</dd>
|
||
<dt><a href="Reference/Global_Objects/TypedArray/sort" title="sort()方法原地排序类型化数组的元素,并且返回类型化数组。这个方法的算法和Array.prototype.sort()相同。 TypedArray 是这里的 类型化数组类型 之一。"><code>Uint16Array.prototype.sort()</code></a></dt>
|
||
<dd>Sorts the elements of an array in place and returns the array. See also <a href="Reference/Global_Objects/Array/sort" title="sort() 方法用原地算法对数组的元素进行排序,并返回数组。排序算法现在是稳定的。默认排序顺序是根据字符串Unicode码点。"><code>Array.prototype.sort()</code></a>.</dd>
|
||
<dt><a href="Reference/Global_Objects/TypedArray/subarray" title="此页面仍未被本地化, 期待您的翻译!"><code>Uint16Array.prototype.subarray()</code></a></dt>
|
||
<dd>Returns a new <code>Uint16Array</code> from the given start and end element index.</dd>
|
||
<dt><a href="Reference/Global_Objects/TypedArray/values" title="values()返回新的 Array Iterator 对象,包含数组中每个下标处的值。"><code>Uint16Array.prototype.values()</code></a></dt>
|
||
<dd>Returns a new <code>Array Iterator</code> object that contains the values for each index in the array. See also <a href="Reference/Global_Objects/Array/values" title="values() 方法返回一个新的 Array Iterator 对象,该对象包含数组每个索引的值"><code>Array.prototype.values()</code></a>.</dd>
|
||
<dt><a href="Reference/Global_Objects/TypedArray/toLocaleString" title="toLocaleString()方法返回一个字符串,表明该类型化数组的元素。这些元素被转化为字符串并由一个区域设置指定的分隔符(例如逗号 “,”)分隔。这个方法与Array.prototype.toLocaleString()拥有相同的算法。同时,由于类型化数组的元素都是数,将每个元素转化为字符串的算法与Number.prototype.toLocaleString()是相同的。(类型化数组)的是typed array types中的其中一个。"><code>Uint16Array.prototype.toLocaleString()</code></a></dt>
|
||
<dd>Returns a localized string representing the array and its elements. See also <a href="Reference/Global_Objects/Array/toLocaleString" title='toLocaleString() 返回一个字符串表示数组中的元素。数组中的元素将使用各自的 toLocaleString 方法转成字符串,这些字符串将使用一个特定语言环境的字符串(例如一个逗号 ",")隔开。'><code>Array.prototype.toLocaleString()</code></a>.</dd>
|
||
<dt><a href="Reference/Global_Objects/TypedArray/toString" title="toString()方法返回一个表示指定数组及其元素的字符串。这个方法的算法和Array.prototype.toString()一样。TypedArray 在这是typed array types 之一。"><code>Uint16Array.prototype.toString()</code></a></dt>
|
||
<dd>Returns a string representing the array and its elements. See also <a href="Reference/Global_Objects/Array/toString" title="toString() 返回一个字符串,表示指定的数组及其元素。"><code>Array.prototype.toString()</code></a>.</dd>
|
||
<dt><a href="Reference/Global_Objects/TypedArray/@@iterator" title="@@iterator 的初始值是和 values 属性的初始值相同的对象。"><code>Uint16Array.prototype[@@iterator]()</code></a></dt>
|
||
<dd>Returns a new <code>Array Iterator</code> object that contains the values for each index in the array.</dd>
|
||
</dl>
|
||
<h2 id="Examples">Examples</h2>
|
||
<p>创建 <code>Uint16Array的不同方法</code>:</p>
|
||
<pre><code class="language-javascript">// From a length
|
||
var uint16 = new Uint16Array(2);
|
||
uint16[0] = 42;
|
||
console.log(uint16[0]); // 42
|
||
console.log(uint16.length); // 2
|
||
console.log(uint16.BYTES_PER_ELEMENT); // 2
|
||
|
||
// From an array
|
||
var arr = new Uint16Array([21,31]);
|
||
console.log(arr[1]); // 31
|
||
|
||
// From another TypedArray
|
||
var x = new Uint16Array([21, 31]);
|
||
var y = new Uint16Array(x);
|
||
console.log(y[0]); // 21
|
||
|
||
// From an ArrayBuffer
|
||
var buffer = new ArrayBuffer(8);
|
||
var z = new Uint16Array(buffer, 0, 4);
|
||
|
||
// From an iterable
|
||
var iterable = function*(){ yield* [1,2,3]; }();
|
||
var uint16 = new Uint16Array(iterable);
|
||
// Uint16Array[1, 2, 3]
|
||
</code></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><a class="external" href="https://www.khronos.org/registry/typedarray/specs/latest/" hreflang="en" lang="en" rel="noopener" title="Typed Array Specification">Typed Array Specification</a></td>
|
||
<td><span class="spec-Obsolete">Obsolete</span></td>
|
||
<td>Superseded by ECMAScript 2015.</td>
|
||
</tr>
|
||
<tr>
|
||
<td><a class="external" href="https://www.ecma-international.org/ecma-262/6.0/#table-49" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">TypedArray constructors</small></a></td>
|
||
<td><span class="spec-Standard">Standard</span></td>
|
||
<td>Initial definition in an ECMA standard. Specified that <code>new</code> is required.</td>
|
||
</tr>
|
||
<tr>
|
||
<td><a class="external" href="https://tc39.github.io/ecma262/#table-49" hreflang="en" lang="en" rel="noopener">ECMAScript Latest Draft (ECMA-262)<br/><small lang="zh-CN">TypedArray constructors</small></a></td>
|
||
<td><span class="spec-Draft">Draft</span></td>
|
||
<td>ECMAScript 2017 changed the <code>Uint16Array</code> constructor to use the <code>ToIndex</code> operation and allows constructors with no arguments.</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<h2 id="Browser_compatibility">Browser compatibility</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 class="external" href="https://github.com/mdn/browser-compat-data" rel="noopener">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</div>
|
||
<p></p><div class="bc-data"><a class="bc-github-link external" href="https://github.com/mdn/browser-compat-data" rel="noopener">Update compatibility data on GitHub</a><table class="bc-table bc-table-js"><thead><tr class="bc-platforms"><td></td><th class="bc-platform-desktop" colspan="6"><span>Desktop</span></th><th class="bc-platform-mobile" colspan="7"><span>Mobile</span></th><th class="bc-platform-server" colspan="1"><span>Server</span></th></tr><tr class="bc-browsers"><td></td><th class="bc-browser-chrome"><span class="bc-head-txt-label bc-head-icon-chrome">Chrome</span></th><th class="bc-browser-edge"><span class="bc-head-txt-label bc-head-icon-edge">Edge</span></th><th class="bc-browser-firefox"><span class="bc-head-txt-label bc-head-icon-firefox">Firefox</span></th><th class="bc-browser-ie"><span class="bc-head-txt-label bc-head-icon-ie">Internet Explorer</span></th><th class="bc-browser-opera"><span class="bc-head-txt-label bc-head-icon-opera">Opera</span></th><th class="bc-browser-safari"><span class="bc-head-txt-label bc-head-icon-safari">Safari</span></th><th class="bc-browser-webview_android"><span class="bc-head-txt-label bc-head-icon-webview_android">Android webview</span></th><th class="bc-browser-chrome_android"><span class="bc-head-txt-label bc-head-icon-chrome_android">Chrome for Android</span></th><th class="bc-browser-edge_mobile"><span class="bc-head-txt-label bc-head-icon-edge_mobile">Edge Mobile</span></th><th class="bc-browser-firefox_android"><span class="bc-head-txt-label bc-head-icon-firefox_android">Firefox for Android</span></th><th class="bc-browser-opera_android"><span class="bc-head-txt-label bc-head-icon-opera_android">Opera for Android</span></th><th class="bc-browser-safari_ios"><span class="bc-head-txt-label bc-head-icon-safari_ios">Safari on iOS</span></th><th class="bc-browser-samsunginternet_android"><span class="bc-head-txt-label bc-head-icon-samsunginternet_android">Samsung Internet</span></th><th class="bc-browser-nodejs"><span class="bc-head-txt-label bc-head-icon-nodejs">Node.js</span></th></tr></thead><tbody><tr><th scope="row"><code>Uint16Array</code></th><td class="bc-supports-yes bc-browser-chrome"><span class="bc-browser-name">Chrome</span><abbr class="bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
</abbr>
|
||
7</td><td class="bc-supports-yes bc-browser-edge"><span class="bc-browser-name">Edge</span><abbr class="bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
</abbr>
|
||
Yes</td><td class="bc-supports-yes bc-browser-firefox"><span class="bc-browser-name">Firefox</span><abbr class="bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
</abbr>
|
||
4</td><td class="bc-supports-yes bc-browser-ie"><span class="bc-browser-name">IE</span><abbr class="bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
</abbr>
|
||
10</td><td class="bc-supports-yes bc-browser-opera"><span class="bc-browser-name">Opera</span><abbr class="bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
</abbr>
|
||
11.6</td><td class="bc-supports-yes bc-browser-safari"><span class="bc-browser-name">Safari</span><abbr class="bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
</abbr>
|
||
5.1</td><td class="bc-supports-yes bc-browser-webview_android"><span class="bc-browser-name">WebView Android</span><abbr class="bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
</abbr>
|
||
4</td><td class="bc-supports-yes bc-browser-chrome_android"><span class="bc-browser-name">Chrome Android</span><abbr class="bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
</abbr>
|
||
Yes</td><td class="bc-supports-yes bc-browser-edge_mobile"><span class="bc-browser-name">Edge Mobile</span><abbr class="bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
</abbr>
|
||
Yes</td><td class="bc-supports-yes bc-browser-firefox_android"><span class="bc-browser-name">Firefox Android</span><abbr class="bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
</abbr>
|
||
4</td><td class="bc-supports-yes bc-browser-opera_android"><span class="bc-browser-name">Opera Android</span><abbr class="bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
</abbr>
|
||
11.6</td><td class="bc-supports-yes bc-browser-safari_ios"><span class="bc-browser-name">Safari iOS</span><abbr class="bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
</abbr>
|
||
4.2</td><td class="bc-supports-yes bc-browser-samsunginternet_android"><span class="bc-browser-name">Samsung Internet Android</span><abbr class="bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
</abbr>
|
||
Yes</td><td class="bc-supports-yes bc-browser-nodejs"><span class="bc-browser-name">nodejs</span><abbr class="bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
</abbr>
|
||
0.10</td></tr><tr><th scope="row"><code>Uint16Array()</code> without <code>new</code> throws</th><td class="bc-supports-yes bc-browser-chrome"><span class="bc-browser-name">Chrome</span><abbr class="bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
</abbr>
|
||
Yes</td><td class="bc-supports-yes bc-browser-edge"><span class="bc-browser-name">Edge</span><abbr class="bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
</abbr>
|
||
Yes</td><td class="bc-supports-yes bc-browser-firefox"><span class="bc-browser-name">Firefox</span><abbr class="bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
</abbr>
|
||
44</td><td class="bc-supports-no bc-browser-ie"><span class="bc-browser-name">IE</span><abbr class="bc-level-no only-icon" title="No support">
|
||
<span>No support</span>
|
||
</abbr>
|
||
No</td><td class="bc-supports-yes bc-browser-opera"><span class="bc-browser-name">Opera</span><abbr class="bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
</abbr>
|
||
Yes</td><td class="bc-supports-unknown bc-browser-safari"><span class="bc-browser-name">Safari</span><abbr title="Compatibility unknown; please update this.">
|
||
?
|
||
</abbr></td><td class="bc-supports-unknown bc-browser-webview_android"><span class="bc-browser-name">WebView Android</span><abbr title="Compatibility unknown; please update this.">
|
||
?
|
||
</abbr></td><td class="bc-supports-unknown bc-browser-chrome_android"><span class="bc-browser-name">Chrome Android</span><abbr title="Compatibility unknown; please update this.">
|
||
?
|
||
</abbr></td><td class="bc-supports-unknown bc-browser-edge_mobile"><span class="bc-browser-name">Edge Mobile</span><abbr title="Compatibility unknown; please update this.">
|
||
?
|
||
</abbr></td><td class="bc-supports-yes bc-browser-firefox_android"><span class="bc-browser-name">Firefox Android</span><abbr class="bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
</abbr>
|
||
44</td><td class="bc-supports-unknown bc-browser-opera_android"><span class="bc-browser-name">Opera Android</span><abbr title="Compatibility unknown; please update this.">
|
||
?
|
||
</abbr></td><td class="bc-supports-unknown bc-browser-safari_ios"><span class="bc-browser-name">Safari iOS</span><abbr title="Compatibility unknown; please update this.">
|
||
?
|
||
</abbr></td><td class="bc-supports-unknown bc-browser-samsunginternet_android"><span class="bc-browser-name">Samsung Internet Android</span><abbr title="Compatibility unknown; please update this.">
|
||
?
|
||
</abbr></td><td class="bc-supports-yes bc-browser-nodejs"><span class="bc-browser-name">nodejs</span><abbr class="bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
</abbr>
|
||
0.12</td></tr><tr><th scope="row">Iterable in constructor</th><td class="bc-supports-unknown bc-browser-chrome"><span class="bc-browser-name">Chrome</span><abbr title="Compatibility unknown; please update this.">
|
||
?
|
||
</abbr></td><td class="bc-supports-unknown bc-browser-edge"><span class="bc-browser-name">Edge</span><abbr title="Compatibility unknown; please update this.">
|
||
?
|
||
</abbr></td><td class="bc-supports-yes bc-browser-firefox"><span class="bc-browser-name">Firefox</span><abbr class="bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
</abbr>
|
||
52</td><td class="bc-supports-unknown bc-browser-ie"><span class="bc-browser-name">IE</span><abbr title="Compatibility unknown; please update this.">
|
||
?
|
||
</abbr></td><td class="bc-supports-unknown bc-browser-opera"><span class="bc-browser-name">Opera</span><abbr title="Compatibility unknown; please update this.">
|
||
?
|
||
</abbr></td><td class="bc-supports-unknown bc-browser-safari"><span class="bc-browser-name">Safari</span><abbr title="Compatibility unknown; please update this.">
|
||
?
|
||
</abbr></td><td class="bc-supports-unknown bc-browser-webview_android"><span class="bc-browser-name">WebView Android</span><abbr title="Compatibility unknown; please update this.">
|
||
?
|
||
</abbr></td><td class="bc-supports-unknown bc-browser-chrome_android"><span class="bc-browser-name">Chrome Android</span><abbr title="Compatibility unknown; please update this.">
|
||
?
|
||
</abbr></td><td class="bc-supports-unknown bc-browser-edge_mobile"><span class="bc-browser-name">Edge Mobile</span><abbr title="Compatibility unknown; please update this.">
|
||
?
|
||
</abbr></td><td class="bc-supports-yes bc-browser-firefox_android"><span class="bc-browser-name">Firefox Android</span><abbr class="bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
</abbr>
|
||
52</td><td class="bc-supports-unknown bc-browser-opera_android"><span class="bc-browser-name">Opera Android</span><abbr title="Compatibility unknown; please update this.">
|
||
?
|
||
</abbr></td><td class="bc-supports-unknown bc-browser-safari_ios"><span class="bc-browser-name">Safari iOS</span><abbr title="Compatibility unknown; please update this.">
|
||
?
|
||
</abbr></td><td class="bc-supports-unknown bc-browser-samsunginternet_android"><span class="bc-browser-name">Samsung Internet Android</span><abbr title="Compatibility unknown; please update this.">
|
||
?
|
||
</abbr></td><td class="bc-supports-yes bc-browser-nodejs"><span class="bc-browser-name">nodejs</span><abbr class="bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
</abbr>
|
||
4.0.0</td></tr><tr><th scope="row">Constructor without arguments</th><td class="bc-supports-unknown bc-browser-chrome"><span class="bc-browser-name">Chrome</span><abbr title="Compatibility unknown; please update this.">
|
||
?
|
||
</abbr></td><td class="bc-supports-unknown bc-browser-edge"><span class="bc-browser-name">Edge</span><abbr title="Compatibility unknown; please update this.">
|
||
?
|
||
</abbr></td><td class="bc-supports-yes bc-browser-firefox"><span class="bc-browser-name">Firefox</span><abbr class="bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
</abbr>
|
||
55</td><td class="bc-supports-unknown bc-browser-ie"><span class="bc-browser-name">IE</span><abbr title="Compatibility unknown; please update this.">
|
||
?
|
||
</abbr></td><td class="bc-supports-unknown bc-browser-opera"><span class="bc-browser-name">Opera</span><abbr title="Compatibility unknown; please update this.">
|
||
?
|
||
</abbr></td><td class="bc-supports-unknown bc-browser-safari"><span class="bc-browser-name">Safari</span><abbr title="Compatibility unknown; please update this.">
|
||
?
|
||
</abbr></td><td class="bc-supports-unknown bc-browser-webview_android"><span class="bc-browser-name">WebView Android</span><abbr title="Compatibility unknown; please update this.">
|
||
?
|
||
</abbr></td><td class="bc-supports-unknown bc-browser-chrome_android"><span class="bc-browser-name">Chrome Android</span><abbr title="Compatibility unknown; please update this.">
|
||
?
|
||
</abbr></td><td class="bc-supports-unknown bc-browser-edge_mobile"><span class="bc-browser-name">Edge Mobile</span><abbr title="Compatibility unknown; please update this.">
|
||
?
|
||
</abbr></td><td class="bc-supports-yes bc-browser-firefox_android"><span class="bc-browser-name">Firefox Android</span><abbr class="bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
</abbr>
|
||
55</td><td class="bc-supports-unknown bc-browser-opera_android"><span class="bc-browser-name">Opera Android</span><abbr title="Compatibility unknown; please update this.">
|
||
?
|
||
</abbr></td><td class="bc-supports-unknown bc-browser-safari_ios"><span class="bc-browser-name">Safari iOS</span><abbr title="Compatibility unknown; please update this.">
|
||
?
|
||
</abbr></td><td class="bc-supports-unknown bc-browser-samsunginternet_android"><span class="bc-browser-name">Samsung Internet Android</span><abbr title="Compatibility unknown; please update this.">
|
||
?
|
||
</abbr></td><td class="bc-supports-unknown bc-browser-nodejs"><span class="bc-browser-name">nodejs</span><abbr title="Compatibility unknown; please update this.">
|
||
?
|
||
</abbr></td></tr></tbody></table><section class="bc-legend" id="sect1"><h3 class="offscreen" id="Legend">Legend</h3><dl><dt><span class="bc-supports-yes bc-supports">
|
||
<abbr class="bc-level bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
|
||
</abbr></span></dt><dd>Full support</dd><dt><span class="bc-supports-no bc-supports">
|
||
<abbr class="bc-level bc-level-no only-icon" title="No support">
|
||
<span>No support</span>
|
||
|
||
</abbr></span></dt><dd>No support</dd><dt><span class="bc-supports-unknown bc-supports">
|
||
<abbr class="bc-level bc-level-unknown only-icon" title="Compatibility unknown">
|
||
<span>Compatibility unknown</span>
|
||
|
||
</abbr></span></dt><dd>Compatibility unknown</dd></dl></section></div><p></p>
|
||
<h2 id="Compatibility_notes">Compatibility notes</h2>
|
||
<p>Starting with ECMAScript 2015, <code>Uint16Array</code> constructors require to be constructed with a <a href="Reference/Operators/new" title="new 运算符创建一个用户定义的对象类型的实例或具有构造函数的内置对象的实例。"><code>new</code></a> operator. Calling a <code>Uint16Array</code> constructor as a function without <code>new</code>, will throw a <a href="Reference/Global_Objects/TypeError" title="TypeError(类型错误) 对象用来表示值的类型非预期类型时发生的错误。"><code>TypeError</code></a> from now on.</p>
|
||
<pre><code class="language-js example-bad">var dv = Uint16Array([1, 2, 3]);
|
||
// TypeError: calling a builtin Uint16Array constructor
|
||
// without new is forbidden</code></pre>
|
||
<pre><code class="language-js example-good">var dv = new Uint16Array([1, 2, 3]);</code></pre>
|
||
<h2 id="See_also">See also</h2>
|
||
<ul>
|
||
<li><a href="/en-US/docs/Web/JavaScript/Typed_arrays" title="en/JavaScript typed arrays">JavaScript typed arrays</a></li>
|
||
<li><a href="Reference/Global_Objects/ArrayBuffer" title="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 https://github.com/mdn/interactive-examples and send us a pull request."><code>ArrayBuffer</code></a></li>
|
||
<li><a href="Reference/Global_Objects/DataView" title="DataView 视图是一个可以从 ArrayBuffer 对象中读写多种数值类型的底层接口,使用它时,不用考虑不同平台的字节序问题。"><code>DataView</code></a></li>
|
||
</ul>
|
||
</article> |