简体   繁体   English

'this' 在节点环境中的行为与浏览器不同

[英]'this' behaves differently in node environment than browser

I am following along with a tutorial about this and execution context .我正在关注有关this执行上下文的教程。 I observed that this code executes properly in the Chrome Console:我观察到这段代码在 Chrome 控制台中正确执行:


var globalThis = this

function myFunc () {  
  console.log('globalThis: ', globalThis)
  console.log('this inside: ', this)
  console.log(globalThis === this)
}

myFunc()

// globalThis: Window {...}
// this inside: Window {...}
// true

However, when I try to execute this same code in a node environment, I get this as a response:但是,当我尝试在节点环境中执行相同的代码时,我得到以下响应:

globalThis:  {}

this inside:  { console: [Getter],
  global: [Circular],
  process: 
   process {
     title: 'node',
     version: 'v8.16.2',
     ...

false

I understand that the global node this value should be different from the browser javascript this value, but the question is, why does myFunc's this value not equal the global value?我理解全局节点this值应该和浏览器javascript this值是不一样的,但是问题来了,为什么myFunc's this值不等于全局值呢?

This is because the global scope of a node module is the same module, so if you execute console.log(this) outside your function you are referencing the global scope of an empty module which is an empty object {} , but when you execute the same console.log(this) inside your function this is going to point to the global Node scope (contains what you are seeing: console, global, process and a lot of other stuff), now if you are on strict mode, the scope inside of your function should be undefined as it doesn't have a default local scope defined. This is because the global scope of a node module is the same module, so if you execute console.log(this) outside your function you are referencing the global scope of an empty module which is an empty object {} , but when you execute function 中的相同console.log(this) this将指向全局节点 scope (包含您所看到的:控制台、全局、进程和许多其他内容),现在如果您处于严格模式, function 内部的 scope 应该undefined ,因为它没有定义默认的本地 scope。

Its a lot simpler on your browser where the global scope is the Window object.它在您的浏览器上简单得多,全局 scope 是Window object。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么forTime的setTimeout内部的IIFE在nodejs中的行为与浏览器的行为不同 - why IIFE inside setTimeout in a for loop behaves differently in nodejs than that of browser “this”关键字在 Nodejs 和浏览器中的行为不同 - The 'this' keyword behaves differently in Nodejs and browser 移动设备浏览器上的输入元素点击行为与 Chrome 设备渲染不同 - Input element click on mobile device browser behaves differently than in Chrome device rendering new Date()在不同环境中的行为不同,为什么? - new Date() behaves differently in different environment, why? Pumpify 在节点 8 和节点 10 中的行为不同 - Pumpify behaves differently in Node 8 and Node 10 lodash是否应该在浏览器中与节点行为不同? - Is lodash find supposed to behave differently in the browser than node? 码头工人中的木偶行为与本地人中的同一个木偶行为不同 - puppeteer in docker behaves differently than the same puppeteer in local jQuery滑块的行为不同于文档中的示例 - Jquery sliders behaves differently than example from documentation $('。input:checked')的行为不同于$('。input [checked = checked]') - $('.input:checked') behaves differently than $('.input[checked=checked]') 正则表达式在 HTML 模式上的表现与在 Express 后端上的表现不同 - Regex Behaves Differently on HTML Pattern than on an Express Backend
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM