Skip to content

Commit 9872c15

Browse files
committed
Merge branch 'gh-pages' of github.com:ruanyf/es6tutorial into gh-pages
2 parents cb02581 + 9068619 commit 9872c15

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

docs/array.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,20 @@ new Array(3).fill(7)
628628

629629
上面代码表示,`fill`方法从 1 号位开始,向原数组填充 7,到 2 号位之前结束。
630630

631+
注意,如果填充的类型为对象,那么被赋值的是同一个内存地址的对象,而不是深拷贝对象。
632+
633+
```javascript
634+
let arr = new Array(3).fill({name: "Mike"});
635+
arr[0].name = "Ben";
636+
arr
637+
// [{name: "Ben"}, {name: "Ben"}, {name: "Ben"}]
638+
639+
let arr = new Array(3).fill([]);
640+
arr[0].push(5);
641+
arr
642+
// [[5], [5], [5]]
643+
```
644+
631645
## 数组实例的 entries(),keys() 和 values()
632646

633647
ES6 提供三个新的方法——`entries()``keys()``values()`——用于遍历数组。它们都返回一个遍历器对象(详见《Iterator》一章),可以用`for...of`循环进行遍历,唯一的区别是`keys()`是对键名的遍历、`values()`是对键值的遍历,`entries()`是对键值对的遍历。

0 commit comments

Comments
 (0)