const doesn't work in Edge 15 developer tools

有些话、适合烂在心里 提交于 2019-12-10 13:34:39

问题


I am running Edge/15.15063. 'Can I Use' says const should work.

Running:

const x = 'woo'

Then:

console.log(x)

Returns

'x' is undefined

Screenshot:

Why isn't const working?


回答1:


I suspect that the Edge console is using a with statement under its covers like other implementations did. This would explain vars and even function declarations being hoisted outside into the global scope, but let and const will be locked into the block scope:

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

Try entering them in multiline mode, in a single evaluation - there they should work.

But you also might want to file a bug, as the console is indeed expected to feel like evaluating things in the global scope.




回答2:


I think I figured this out, but this is as much a guess as an answer. Too long for a comment though.

I think what's happening is that const and let do not create implicit globals when used in the top-level scope in the same way var does. Although top-level variables created with const and let are global, they are not properties of the global window object.

If the MS console is relying on that implicit window property creation for accessing variables created in the console, then const and let will not work.

I am unsure of the inner workings of Chrome Dev Tools, but it seems to create an anonymous function wrapper for code executed in the console:

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

I am unsure if there is other sandboxing going on here, I didn't necessarily find a lot of documentation on it.



来源:https://stackoverflow.com/questions/44286729/const-doesnt-work-in-edge-15-developer-tools

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!