Int16Array
Int16Array
该Int16Array
类型数组代表二进制补码16位有符号整数形式的字节的数组。如果需要控制字节顺序,则使用DataView
。初始化内容为0
。一旦数组建立,您可以使用数组的方法或使用标准数组索引(即使用括号表示法)引用数组中的元素。
语法
new Int16Array( // new in ES2017
new Int16Array(length
new Int16Array(typedArray
new Int16Array(object
new Int16Array(buffer [, byteOffset [, length]]
有关构造函数语法和参数的更多信息,请参阅TypedArray
。
属性
Int16Array.BYTES_PER_ELEMENT
返回元素大小的数字值。
方法
Int16Array.from()Int16Array
从类似数组或类的对象中创建一个新的对象。另见Array.from()
。
Int16Array 属性
All Int16Array
objects inherit from %TypedArray%.prototype
.
属性
Int16Arra.prototype.constructor
返回创建实例原型的函数,这是Int16Array默认的构造函数
方法
Int16Array.prototype.copyWithin()
复制数组中的一系列数组元素。另见Array.prototype.copyWithin()
。
示例
创造 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]
规范
Specification | Status | Comment |
---|---|---|
Typed Array Specification | Obsolete | Superseded by ECMAScript 2015. |
ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'TypedArray constructors' in that specification. | Standard | Initial definition in an ECMA standard. Specified that new is required. |
ECMAScript Latest Draft (ECMA-262)The definition of 'TypedArray constructors' in that specification. | Draft | ECMAScript 2017 changed the Int16Array constructor to use the ToIndex operation and allows constructors with no arguments. |
浏览器兼容性
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 7.0 | (Yes) | 4.0 (2) | 10 | 11.6 | 5.1 |
new is required | ? | ? | 44 (44) | ? | ? | ? |
Iterable in constructor | ? | ? | 52 (52) | ? | ? | ? |
Constructor without arguments | ? | ? | 55 (55) | ? | ? | ? |
Feature | Android | Chrome for Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | 4.0 | (Yes) | (Yes) | 4.0 (2) | 10 | 11.6 | 4.2 |
new is required | ? | ? | ? | 44.0 (44) | ? | ? | ? |
Iterable in constructor | ? | ? | ? | 52.0 (52) | ? | ? | ? |
Constructor without arguments | ? | ? | ? | 55.0 (55) | ? | ? | ? |