为什么我可以在导出模块中的声明之前访问const

Mih*_*scu 1 import module export hoisting ecmascript-6

我有这个问题,因为 const 变量没有被提升并且应该抛出语法错误。

为什么这段代码有效?

export let testModule = () => {
    let test = document.querySelector('.test');
    test.innerHTML = myText;
    test.style.cssText = 'color: red';
}

const myText = 'IT WORKS!!!!!'
Run Code Online (Sandbox Code Playgroud)

如果您想亲自检查,我已附上工作文件:https://plnkr.co/edit/TR8SvCjQgqPDWpI3 ?preview

jfr*_*d00 5

为什么我可以在导出模块中的声明之前访问const

因为testModule()直到稍后才会被调用,并且const myText在调用函数时已在可到达的范围内定义。