JavaScript 中的 this 详解
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 首先,我知道这篇文章很无聊,无非就是关于 js 中的 this,并且也已经有千千万万的文章写过这部分内容了; 但是,我还是想写一篇关于 js 中的 this 的文章,算是一个总结归纳吧;(大神们可以绕行) 在 js 中,this 这个上下文总是变化莫测,很多时候出现 bug 总是一头雾水,其实,只要分清楚不同的情况下如何执行就 ok 了。 全局执行 首先,我们在全局环境中看看它的 this 是什么: first. 浏览器: console.log( this ); // Window {speechSynthesis: SpeechSynthesis, caches: CacheStorage, localStorage: Storage, sessionStorage: Storage, webkitStorageInfo: DeprecatedStorageInfo…} second. node: console.log( this ); // global 可以看到打印出了 global 对象; 总结 :在全局作用域中它的 this 执行当前的全局对象(浏览器端是 Window ,node 中是 global )。 函数中执行 纯粹的函数调用 这是最普通的函数使用方法了: function test()