I have created a script which logs the this variable to the console in the global scope. If I launch the script in the browser it returns undefined. eg.
console.log(this); //returns undefined
However if I enter the same command directly in the browser console it returns the window object. Which is what I was expecting. eg.
console.log(this); //returns Window {postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, frames: Window, …}
Why is the behavior/output of logging 'this' to the console different in the browser console and different while inspecting the output from a script?
Edit: I am compiling the script with babel (es2015 preset). As far as my initial question is concerned the issue is now resolved. Disabling the Babel preset/plugins results in the expected output of the this variable.
此外,如果您使用的是 ES6 模块,则会自动启用严格模式,如果在全局范围内使用它,它将返回 undefined。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.