这是箭头函数在浏览器中是全局的但在 NodeJS 中不是
this is Arrow functions in browser is global but not in NodeJS
在Googlechrome
const ExampleObject5 = () => {
console.log(this===window)
}
ExampleObject5()
打印为真
但在节点
const ExampleObject5 = () => {
console.log(this===global)
}
ExampleObject5()
打印错误
通过阅读NodeJS Documentation:
global
The global namespace object.
In browsers, the top-level scope is the global scope. That means that
in browsers if you're in the global scope var something will define a
global variable. In Node.js this is different. The top-level scope is
not the global scope; var something inside an Node.js module will be
local to that module.
因此在 Node 中,顶级作用域不是全局作用域(如在浏览器中),而是模块本身。
在Googlechrome
const ExampleObject5 = () => {
console.log(this===window)
}
ExampleObject5()
打印为真
但在节点
const ExampleObject5 = () => {
console.log(this===global)
}
ExampleObject5()
打印错误
通过阅读NodeJS Documentation:
global
The global namespace object.
In browsers, the top-level scope is the global scope. That means that in browsers if you're in the global scope var something will define a global variable. In Node.js this is different. The top-level scope is not the global scope; var something inside an Node.js module will be local to that module.
因此在 Node 中,顶级作用域不是全局作用域(如在浏览器中),而是模块本身。