我正在使用for in
循环来迭代像这样的对象中的键
var Obj = {
foo: 'bar',
num: '1234'
}
for(key in Obj){
console.log(key)
}
哪个输出
foo
bar
然而,当我在类方法中放入完全相同的代码时,我得到了
ReferenceError: key is not defined
我所指的类是通过它自己的模块导出的。 (不确定这是否重要,因为我无法在线找到有关此行为的任何信息)
那么为什么不能在类中使用in循环呢?
使用Node V 8.6.0
答案 0 :(得分:1)
使用let或var
for(let key in Obj){
console.log(key)
}