JavaScript中Array.prototype.fill()的用法

看了一下mdn中的对fill()解释和语法

代码参考:

var array1 = [1, 2, 3, 4];

// fill with 0 from position 2 until position 4
console.log(array1.fill(0, 2, 4));//0表示数组元素转换的值,2表示从数组索引为2的位置开始,4表示数组长度为4
// expected output: [1, 2, 0, 0]

// fill with 5 from position 1
console.log(array1.fill(5, 1));//5表示数组后面转换的元素值为5,1表示从数组索引为1的位置开始,后面的元素都转换为5
// expected output: [1, 5, 5, 5]

console.log(array1.fill(6));//6表示数组需要转换的值是6,因为这边没有些元素转换的索引位置,所以默认数组的所有元素都是6
// expected output: [6, 6, 6, 6]

如果我的理解有错,希望大家可以指出来。。。谢谢啦。我也是跟着自己的理解写出来的,可能我的理解会有些错。。。。 

猜你喜欢

转载自blog.csdn.net/DiegoBrother/article/details/83713659