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

Why can I access const before declaration in exported module

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

为什么这段代码有效?

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

const myText = 'IT WORKS!!!!!'

如果你想自己检查一下,我已经附上了工作代码:https://plnkr.co/edit/TR8SvCjQgqPDWpI3?preview

Why can I access const before declaration in exported module

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