有什么方法可以在 Chrome DevTools 中重新分配 const 吗?

Is there any way to re-assign a const in Chrome DevTools?

我知道 const 值并不意味着要重新分配,但是考虑到 DevTools 是为了在调试时调整值,我认为应该有一种快速的方法来尝试值。让我们说 app.js:

const foo = 5;

在控制台中,我尝试在控制台中重新分配它foo = 6;

显然它不起作用(赋值给常量变量)。我也尝试过 delete(window.foo) 但即使 delete 函数 returns 为真,该值仍然存在。

目前的解决方法是使用本地覆盖(并且必须设置持久文件系统),因为实时编辑将不起作用(该值已经存在并且没有本地覆盖,编辑后的值在重新加载后无法保存)。

有没有更简单的方法来快速更改 const 值?

您不能删除或更新常量变量,但可以删除或更新常量对象的属性。 如果可以更新或删除变量,则 const 关键字的用途将会改变。这是不可能的。您只能执行以下操作。

const foo=10; 
function changeConst(){
                       const foo=99;
                       console.log(foo);
                      };
changeConst();

Google Chrome DevTools group上的解法:

You can actually do that, just not using the console. If you have a breakpoint before the code that uses the value that you want to change, let it hit and then double click on the value of the variable under the Scope drawer and edit it.