在浏览器窗口中,当我在窗口上下文中记录this
时,它会给出输出:Window对象。
但是在这里,在普通的 IDE(如 Visual Studio 代码)中,当我编译它时,给我一个空对象。为什么?这是如何发生的?
console.log(this) // in browsers output: window
// In IDE, output: {}
function test(){
return this
}
console.log(test()) // output is global // Why not empty
主要问题是,当我在普通 IDE 上运行时,为什么 this
返回一个空对象?谢谢您的回答。
最佳答案
this
引用 console.log(this)
中的全局范围。对于浏览器,它是window
,对于nodejs,它是Global
对象。对于 VS Code,它们有空对象作为全局对象,因为它们无法实现浏览器或 NodeJS 全局对象 api 和属性。由于它是用于调试的,所以空的全局对象就足够了。
关于javascript - 为什么在常规 IDE(如 Visual Studio 代码)上编译时 `this` 返回空对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57214221/