在线文档教程

Uint8ClampedArray

Uint8ClampedArray

Uint8ClampedArray(8位无符号整型固定数组)类型化数组表示一个由值固定在0-255区间的8位无符号整型组成的数组;如果你指定一个在 [0,255] 区间外的值,它将被替换为0或255;如果你指定一个非整数,那么它将被设置为最接近它的整数。(数组)内容被初始化为0。一旦(数组)被创建,你可以使用对象的方法引用数组里的元素,或使用标准的数组索引语法(即使用方括号标记)。

语法

new Uint8ClampedArray( // new in ES2017 new Uint8ClampedArray(length new Uint8ClampedArray(typedArray new Uint8ClampedArray(object new Uint8ClampedArray(buffer [, byteOffset [, length]]

关于构造函数语法和参数的更多信息,参见TypedArray

属性

Uint8ClampedArray.BYTES_PER_ELEMENT返回元素大小的一个数值。对Uint8ClampedArray而言是1。

方法

Uint8ClampedArray.from()从一个类数组或可枚举对象创建一个新的Uint8ClampedArray。参见Array.from()

Uint8ClampedArray prototype

所有的 Uint8ClampedArray 对象继承自 %TypedArray%.prototype

属性

Uint8ClampedArray.prototype.constructor返回创建一个实例原型的函数。这是Uint8ClampedArray默认的构造函数。

Methods

Uint8ClampedArray.prototype.copyWithin()复制数组中的一系列数组元素。另见Array.prototype.copyWithin()

示例

不同的方式来创建一个Uint8ClampedArray

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

规范

SpecificationStatusComment
Typed Array SpecificationObsoleteSuperseded by ECMAScript 2015.
ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'TypedArray constructors' in that specification.StandardInitial definition in an ECMA standard. Specified that new is required.
ECMAScript Latest Draft (ECMA-262)The definition of 'TypedArray constructors' in that specification.DraftECMAScript 2017 changed the Uint8ClampedArray constructor to use the ToIndex operation and allows constructors with no arguments.

浏览器兼容性

FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support7.0(Yes)4.0 (2)11 (as of KB2929437)11.65.1
new is required??44 (44)???
Iterable in constructor??52 (52)???
Constructor without arguments??55 (55)???

FeatureAndroidChrome for AndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support4.0(Yes)(Yes)4.0 (2)(Yes)11.64.2
new is required???44.0 (44)???
Iterable in constructor???52.0 (52)???
Constructor without arguments???55.0 (55)???