如何在不刷新页面的情况下重置Firefox控制台?

pyk*_*yke 6 firefox firefox-developer-tools

如何重置Firefox Web控制台以便能够使用已经声明的变量?

console.clear()只会清除输出。已经声明的所有变量都保留在内存中。我正在努力摆脱:

Error: "SyntaxError: redeclaration of..."
Run Code Online (Sandbox Code Playgroud)

Sum*_*ai8 7

When testing something, use the same construction you would (well, should) use to prevent code and variables being dropped in the global scope. Use Immediately Invoked Function Expressions (IIFE) and place your code in it. Each IIFE is it's own context, so re-declaring is not a problem.

(function () {
  const myConstant = 1;

  console.log(myConstant);
}());
Run Code Online (Sandbox Code Playgroud)

Note that Chrome 80 allows redeclaration of const and let. There exists a relevant bug in bugzilla for Firefox.

  • 更简单地说,您可以将代码括在大括号内:`{ // 很多代码}`。 (7认同)

Fat*_*imi 6

如果刷新页面会重置控制台,那么您可以使用location.reload()通过控制台刷新页面。