问题
What is variable declared within file scope in javascript?
Is there anything file scope, considering multiple files are used in an app.
回答1:
ES6 modules form their own file scope (as if the entire contents of the file were wrapped in a function).
Variables declared in a module are completely inaccessible from outside that module (unless they're exported).
回答2:
In JavaScript, there are only 3 types of scope:
- Global Scope (i.e. every variable/function defined outside functions in a file or multiple files)
- Functional Scope (i.e. every variable/function defined within the function)
- Closure Scope (i.e. code block/function having access to its surrounding lexical scope)
来源:https://stackoverflow.com/questions/44916179/what-is-file-scope-in-javascript