在 class 中使用 for in 循环会导致引用错误
Using a for in loop inside a class results in a reference error
我正在使用 for in
循环来迭代对象中的键,就像这样
var Obj = {
foo: 'bar',
num: '1234'
}
for(key in Obj){
console.log(key)
}
输出
foo
bar
但是,当我将完全相同的代码放入 class 方法时,我得到
ReferenceError: key is not defined
我指的 class 是通过它自己的模块导出的。 (不确定这是否重要,因为我无法在网上找到有关此行为的任何信息)
那么为什么不能在 classes 中使用 for in 循环?
使用节点 V 8.6.0
使用 let 或 var
for(let key in Obj){
console.log(key)
}
我正在使用 for in
循环来迭代对象中的键,就像这样
var Obj = {
foo: 'bar',
num: '1234'
}
for(key in Obj){
console.log(key)
}
输出
foo
bar
但是,当我将完全相同的代码放入 class 方法时,我得到
ReferenceError: key is not defined
我指的 class 是通过它自己的模块导出的。 (不确定这是否重要,因为我无法在网上找到有关此行为的任何信息)
那么为什么不能在 classes 中使用 for in 循环?
使用节点 V 8.6.0
使用 let 或 var
for(let key in Obj){
console.log(key)
}