【发布时间】:2018-05-23 08:37:47
【问题描述】:
这是源代码:
const james = {
name: 'James',
height: `5'10"`,
weight: 185,
[Symbol.iterator]:function*(){
yield Object.keys(this) ;
}
};
const iterator = james[Symbol.iterator]();
//
console.log(iterator.next().value); // 'James'
console.log(iterator.next().value); // `5'10`
console.log(iterator.next().value); // 185
第一次调用iterator.next().value 应该打印
{"value":"James","key":"name","done":false}
但它正在打印{"value":["name","height","weight"],"done":false}。如何解决这个问题?
【问题讨论】:
-
迭代结果中的
key来自哪里?Object.keys生成数组["name","height","weight"],仅此而已。
标签: javascript ecmascript-6 iterator iterable