const 在 Edge 15 开发者工具中不起作用

const doesn't work in Edge 15 developer tools

我是运行Edge/15.15063。 'Can I Use' says const should work.

运行:

const x = 'woo'

然后:

console.log(x)

Returns

'x' is undefined

截图:

为什么 const 不起作用?

我想我明白了,但这既是猜测也是答案。不过评论太长了。

认为 发生的事情是 constlet do not create 在相同的顶级范围内使用时隐式全局变量var 的方式。尽管使用 constlet 创建的顶级变量是全局的,但它们不是全局 window 对象的属性。

如果 MS 控制台依赖于隐式 window 属性 创建来访问在控制台中创建的变量,那么 constlet 将不起作用。

我不确定 Chrome Dev Tools 的内部工作原理,但它似乎为在控制台中执行的代码创建了一个匿名函数包装器:

throw new Error;

VM679:1 Uncaught Error at anonymous:1:7

(function() { throw new Error; })();

VM759:1 Uncaught Error at anonymous:1:21 at anonymous:1:33

我不确定这里是否还有其他沙盒,我没有找到很多关于它的文档。

我怀疑 Edge 控制台在其掩护下使用 with 语句 like other implementations did. This would explain vars and 被提升到全局范围之外,但是 letconst 将锁定到块范围:

with (…) {
    const x = 'woo'
}
// next input:
with (…) {
    console.log(x) // obviously undeclared
}

尝试在单次评估中以多行模式输入它们 - 它们应该可以工作。

但您可能还想提交一个错误,因为确实希望控制台 感觉 喜欢在全局范围内评估事物。