TypedArray.prototype.fill()

TypedArray 实例的 fill() 方法将类型化数组中索引范围内的所有元素更改为静态值。它返回修改后的类型化数组。该方法与 Array.prototype.fill() 的算法相同。

¥The fill() method of TypedArray instances changes all elements within a range of indices in a typed array to a static value. It returns the modified typed array. This method has the same algorithm as Array.prototype.fill().

Try it

语法

¥Syntax

js
fill(value)
fill(value, start)
fill(value, start, end)

参数

¥Parameters

value

用于填充类型化数组的值。

start Optional

开始填充的从零开始的索引,转换为整数

end Optional

结束填充的从零开始的索引,转换为整数fill() 填充至但不包括 end

返回值

¥Return value

修改后的类型化数组,用 value 填充。

¥The modified typed array, filled with value.

描述

¥Description

详细信息请参见 Array.prototype.fill()。此方法不是通用的,只能在类型化数组实例上调用。

¥See Array.prototype.fill() for more details. This method is not generic and can only be called on typed array instances.

示例

¥Examples

使用 fill()

¥Using fill()

js
new Uint8Array([1, 2, 3]).fill(4); // Uint8Array [4, 4, 4]
new Uint8Array([1, 2, 3]).fill(4, 1); // Uint8Array [1, 4, 4]
new Uint8Array([1, 2, 3]).fill(4, 1, 2); // Uint8Array [1, 4, 3]
new Uint8Array([1, 2, 3]).fill(4, 1, 1); // Uint8Array [1, 2, 3]
new Uint8Array([1, 2, 3]).fill(4, -3, -2); // Uint8Array [4, 2, 3]

规范

Specification
ECMAScript Language Specification
# sec-%typedarray%.prototype.fill

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看