TypeError:如果运行ES6代码,则在Firebug控制台中重新声明let错误 [英] TypeError: redeclaration of let error in Firebug console if running ES6 code

查看:267
本文介绍了TypeError:如果运行ES6代码,则在Firebug控制台中重新声明let错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下是运行正常的代码,如果我点击运行按钮一次,但是在第二次打开它开始显示 TypeError:重新声明让我的arr 错误。



让我知道这个奇怪的(可能不是)行为。

  let myArr = [34,45,67,2,67,1,5 ,90]; 
let evenArr = [];
让oddArr = [];
myArr.forEach(x => {
if(x%2 === 0){
evenArr.push(x);
} else {
oddArr.push(x);
}
});

console.log(evenArr);
console.log(oddArr);

错误 -



解决方案

ES6 不允许您执行此操作(在相同范围内重新声明块范围变量):

  let foo; 
让foo;

由于控制台保持状态,直到您重新加载页面(您在页面的上下文中所有),你第一次运行它定义 myArr ,所以你不能在第二次运行时重新定义它。


I am learning ES6, so bear me please.

Following is the code which is running fine, if I click the Run button one time, but on second hit it starts showing TypeError: redeclaration of let myArr error.

Let me know about this weird (may be not) behavior.

let myArr = [34,45,67,2,67,1,5,90];
let evenArr = [];
let oddArr = [];
myArr.forEach(x => {
    if (x % 2 === 0) {
        evenArr.push(x);
    } else {
        oddArr.push(x);
    }
});

console.log(evenArr);
console.log(oddArr);

Error -

解决方案

ES6 does not allow you to do this (redeclaring a block-scoped variable in the same scope):

let foo;
let foo;

And since the console keeps state until you reload the page (you are in the context of the page after all), the first time you run it you define myArr so you cannot redefine it on the second run.

这篇关于TypeError:如果运行ES6代码,则在Firebug控制台中重新声明let错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆