對象遍歷
| 方法 | 是否可遍歷不可枚舉屬性 | 是否可遍歷Symbol屬性 | 是否可原型鏈屬性 |
|---|---|---|---|
| Object.keys | --- | --- | --- |
| for...in | --- | --- | true |
| Object.getOwnPropertyNames | true | --- | --- |
| Object.prototype.hasOwnProperty | true | true | --- |
| Object.assign | --- | true | --- |
| JSON.stringify | --- | --- | --- |
| Reflect.ownKeys | true | true | --- |
| Object.getOwnPropertySymbols | --- | true | --- |
數(shù)組遍歷中各個方法的特點(diǎn)
- Array.prototype.forEach:
- 無法跳出循環(huán)
- for...in:
- 數(shù)組的鍵名是數(shù)字,但是for...in循環(huán)是以字符串作為鍵名“0”、“1”、“2”等等。
- for...in循環(huán)不僅遍歷數(shù)字鍵名,還會遍歷手動添加的其他鍵,甚至包括原型鏈上的鍵。
- for...of:
- 有著同for...in一樣的簡潔語法,但是沒有for...in那些缺點(diǎn)。
- 不同于forEach方法,它可以與break、continue和return配合使用。
- 提供了遍歷所有數(shù)據(jù)結(jié)構(gòu)的統(tǒng)一操作接口。