Int16Array() typed array constructor creates an array of twos-complement 16-bit signed integers in the platform byte order. If control over byte order is needed, use DataView instead. The contents are initialized to 0 . 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).

句法

new Int16Array(); // new in ES2017
new Int16Array(length);
new Int16Array(typedArray);
new Int16Array(object);
new Int16Array(buffer [, byteOffset [, length]]);
					

参数

length
When called with a length argument, an internal array buffer is created in memory, of size length multiplied by BYTES_PER_ELEMENT bytes, containing zeros.
typedArray
When called with a typedArray argument, which can be an object of any of the typed array types (such as Int32Array ), the typedArray gets copied into a new typed array. Each value in typedArray is converted to the corresponding type of the constructor before being copied into the new array. The length of the new typed array will be same as the length of the typedArray 自变量。
object
When called with an object argument, a new typed array is created as if by the TypedArray .from() 方法。
buffer , byteOffset , length
When called with a buffer , and optionally a byteOffset length argument, a new typed array view is created that views the specified ArrayBuffer byteOffset and length parameters specify the memory range that will be exposed by the typed array view.  If both are omitted, all of buffer is viewed; if only length is omitted, the remainder of buffer is viewed.

范例

Different ways to create an Int16Array

// From a length
var int16 = new Int16Array(2);
int16[0] = 42;
console.log(int16[0]); // 42
console.log(int16.length); // 2
console.log(int16.BYTES_PER_ELEMENT); // 2
// From an array
var arr = new Int16Array([21,31]);
console.log(arr[1]); // 31
// From another TypedArray
var x = new Int16Array([21, 31]);
var y = new Int16Array(x);
console.log(y[0]); // 21
// From an ArrayBuffer
var buffer = new ArrayBuffer(8);
var z = new Int16Array(buffer, 0, 4);
// From an iterable
var iterable = function*(){ yield* [1,2,3]; }();
var int16 = new Int16Array(iterable);
// Int16Array[1, 2, 3]
					

规范

规范
ECMAScript (ECMA-262)
The definition of 'TypedArray constructors' in that specification.

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
Int16Array() 构造函数 Chrome 7 Edge 12 Firefox 4 IE 10 Opera 11.6 Safari 5.1 WebView Android 4 Chrome Android 18 Firefox Android 4 Opera Android 12 Safari iOS 4.2 Samsung Internet Android 1.0 nodejs 0.10
Constructor without arguments Chrome 7 Edge 12 Firefox 55 IE 10 Opera 11.6 Safari 5.1 WebView Android ≤37 Chrome Android 18 Firefox Android 55 Opera Android 12 Safari iOS 5 Samsung Internet Android 1.0 nodejs 0.10
new Int16Array(iterable) Chrome 39 Edge 14 Firefox 52 IE No Opera 26 Safari 10 WebView Android 39 Chrome Android 39 Firefox Android 52 Opera Android 26 Safari iOS 10 Samsung Internet Android 4.0 nodejs 4.0.0
Int16Array() without new throws Chrome 7 Edge 14 Firefox 44 IE No Opera 15 Safari 5.1 WebView Android ≤37 Chrome Android 18 Firefox Android 44 Opera Android 14 Safari iOS 5 Samsung Internet Android 1.0 nodejs 0.12

图例

完整支持

完整支持

不支持

不支持

Compatibility notes

Starting with ECMAScript 2015, Int16Array constructors require to be constructed with a new operator. Calling a Int16Array constructor as a function without new , will throw a TypeError from now on.

var dv = Int16Array([1, 2, 3]);
// TypeError: calling a builtin Int16Array constructor
// without new is forbidden
					
var dv = new Int16Array([1, 2, 3]);
					

另请参阅

元数据

  • 最后修改:
  1. 标准内置对象
  2. TypedArray
  3. 特性
    1. TypedArray.BYTES_PER_ELEMENT
    2. TypedArray.name
    3. TypedArray.prototype.buffer
    4. TypedArray.prototype.byteLength
    5. TypedArray.prototype.byteOffset
    6. TypedArray.prototype.length
    7. get TypedArray[@@species]
  4. 方法
    1. TypedArray.from()
    2. TypedArray.of()
    3. TypedArray.prototype.copyWithin()
    4. TypedArray.prototype.entries()
    5. TypedArray.prototype.every()
    6. TypedArray.prototype.fill()
    7. TypedArray.prototype.filter()
    8. TypedArray.prototype.find()
    9. TypedArray.prototype.findIndex()
    10. TypedArray.prototype.forEach()
    11. TypedArray.prototype.includes()
    12. TypedArray.prototype.indexOf()
    13. TypedArray.prototype.join()
    14. TypedArray.prototype.keys()
    15. TypedArray.prototype.lastIndexOf()
    16. TypedArray.prototype.map()
    17. TypedArray.prototype.reduce()
    18. TypedArray.prototype.reduceRight()
    19. TypedArray.prototype.reverse()
    20. TypedArray.prototype.set()
    21. TypedArray.prototype.slice()
    22. TypedArray.prototype.some()
    23. TypedArray.prototype.sort()
    24. TypedArray.prototype.subarray()
    25. TypedArray.prototype.toLocaleString()
    26. TypedArray.prototype.toString()
    27. TypedArray.prototype.values()
    28. TypedArray.prototype[@@iterator]()
  5. 相关页面:
  6. Int8Array
  7. Uint8Array
  8. Uint8ClampedArray
  9. Int16Array
  10. Uint16Array
  11. Int32Array
  12. Uint32Array
  13. Float32Array
  14. Float64Array
  15. BigInt64Array
  16. BigUint64Array
  17. 继承:
  18. Function
  19. 特性
    1. Function.arguments
    2. Function.caller
    3. Function.displayName
    4. Function.length
    5. Function.name
  20. 方法
    1. Function.prototype.apply()
    2. Function.prototype.bind()
    3. Function.prototype.call()
    4. Function.prototype.toSource()
    5. Function.prototype.toString()
  21. Object
  22. 特性
    1. Object.prototype.__proto__
    2. Object.prototype.constructor
  23. 方法
    1. Object.prototype.__defineGetter__()
    2. Object.prototype.__defineSetter__()
    3. Object.prototype.__lookupGetter__()
    4. Object.prototype.__lookupSetter__()
    5. Object.prototype.hasOwnProperty()
    6. Object.prototype.isPrototypeOf()
    7. Object.prototype.propertyIsEnumerable()
    8. Object.prototype.toLocaleString()
    9. Object.prototype.toSource()
    10. Object.prototype.toString()
    11. Object.prototype.valueOf()
    12. Object.setPrototypeOf()