SOURCE

// https://zhuanlan.zhihu.com/p/161892289
// https://es6.ruanyifeng.com/#docs/iterator

// const obj = {a: 1, b: 2, c: 3}
// for (let i in obj) {
//     console.log(`键名:${i},键值:${obj[i]}`)
// }
// for (let key of obj) {
//     console.log(key)
// }

// 对象不能使用for...of来遍历


// const arr = ['张三', '李四', '王五']
// for (let i in arr) {
//     console.log(`索引:${i};值是:${arr[i]}`)
// }
// for (let v of arr) {
//     console.log(v)
// }

// 使用for...of来遍历数组,遍历的是是数组中每一项的值


// const arr = ['张三', '李四', '王五']
// arr.name = '这是一个数组'
// for (let i in arr) {
//     console.log(i)
// }
// for (let v of arr) {
//     console.log(v)
// }

// 使用for...in来遍历数组,在数组上动态添加的属性也会被遍历到
// 作为对比,使用for...of来遍历数组,在数组上动态添加的属性不会被遍历到

// const arr = ['张三', '李四', '王五']
// // 在原型上定义了一个method方法
// Array.prototype.method = function () {
//     console.log('挂载到数组原型上的方法')
// }
// // 给arr增加了一个name属性
// arr.name = '这是一个数组'
// for (let i in arr) {
//     console.log(i)
// }

// 使用for...in来遍历数组,在数组原型上添加的方法或者在数组上添加的属性都会被遍历到


// 特别情况下, for ... in 循环会以看起来任意的顺序遍历键名
// function Demo() {
//     this[100] = 'test-100'
//     this[99] = 'test-99'
//     this[55] = 'test-55'
//     this[66] = 'test-66'
//     this['B'] = 'test-B'
//     this['C'] = 'test-C'
//     this['A'] = 'test-A'
// }

// const d = new Demo()

// for (let key in d) {
//     console.log(key)
// }


// 使用for...of来遍历一个数组,相比于使用es5语法forEach,使用for...of来遍历数组,可以使用continue、break、return来终止遍历过程
// 对象不能使用for...of循环来遍历
// 能够使用for...of来遍历的数据必须具有Symbol.iterator 属性
// 包括 数组、字符串、伪数组、Map和Set这两种新的数据结构


// 我要想使用for...of循环来遍历对象,要怎么操作
// 对象不能使用for...of循环
// 先使用Object.keys()将对象的键名收集起来,是一个数组,数组是可以使用for...of循环的
// const obj = {a:1, b:2, c:3}
// // for (let v of obj) {

// // }
// console.log(Object.keys(obj))
// for (let v of Object.keys(obj)) {
//     console.log(`键名:${v};键值:${obj[v]}`)
// }


// 上面说String是可遍历的,也就是String数据有Symbol.iterator属性
// const str = 'helloworld!'
// for (let v of str) {
//     console.log(v)
// }
console 命令行工具 X clear

                    
>
console