1
0
mirror of https://github.com/ruanyf/es6tutorial.git synced 2025-05-24 18:32:22 +00:00

Update Array

对Array.prototype.fill 方法作补充
This commit is contained in:
picc-lu 2018-01-27 16:48:47 +08:00 committed by GitHub
parent b2a2c581bb
commit c669887292
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -628,6 +628,20 @@ new Array(3).fill(7)
上面代码表示,`fill`方法从 1 号位开始,向原数组填充 7到 2 号位之前结束。
注意,如果填充的类型为对象,那么被赋值的是同一个内存地址的对象,而不是深拷贝对象。
```javascript
let arr = new Array(3).fill({name: "Mike"});
arr[0].name = "Ben";
arr
// [{name: "Ben"}, {name: "Ben"}, {name: "Ben"}]
let arr = new Array(3).fill([]);
arr[0].push(5);
arr
// [[5], [5], [5]]
```
## 数组实例的 entries()keys() 和 values()
ES6 提供三个新的方法——`entries()``keys()``values()`——用于遍历数组。它们都返回一个遍历器对象详见《Iterator》一章可以用`for...of`循环进行遍历,唯一的区别是`keys()`是对键名的遍历、`values()`是对键值的遍历,`entries()`是对键值对的遍历。