Differences between this keyword in NodeJS and browsers

Differences between this keyword in NodeJS and browsers

Preface

Anyone who has learned JavaScript must be aware of the issue of where this points to in different environments. Then look at the following code

var type = 1
function toWhere(){
	this.type = 2;
}
toWhere();
console.log(type)

You will definitely think:

A global variable type is declared here. When type=1 is executed, the value is assigned to 1. After that, the toWhere function is called. When we see this in the function, we determine where this points to. It is very clear here that this points to window. After this.type=2 is executed, the global variable type is assigned a value of 2.

Finally, the global variable type is printed and the result is obviously 2.

Open the browser to verify, and there is a 2 clearly there.

So is this the end?

If you have learned node, and now re-execute the above code with nodejs, you will find the difference.

Now you find that the 1 is wrong. Isn’t it equal to 2?

Related debugging

From the above examples, we can see that the same js code has different results when it is run in the browser and in nodejs.

This is actually because of the problem of this pointing, but this pointing is different from what we usually know. This pointing problem is caused by the working principle of node

var type = 1
function toWhere() {
 this.type = 2
 console.log("this points to in the function", this)
}
toWhere()
console.log(type)
console.log("globally this", this)

1. Print this in the browser

This in the function points to window, and the global this also points to window

2. Print this in nodeJs

Found it. This in the function points to Object [global].

When we assign a value to a function's this, it is actually attached to the global object. So it will not change the value of this in the global

Node principle analysis

So let's see why this is happening.

First we need to understand how nodeJs works

Script files that are executed directly by the browser in the global scope

In Node, Node hides the code in an anonymous function that is called immediately. You can use global to access the global scope.

In the previous explanation, we found that a this printed externally points to an empty object {}. In fact, any file running in node is actually wrapped in a {}, so the script files are executed in their own closures, similar to the following

{
	(function(){
		//Script file })()
}

In the previous example, outside the function this refers to an empty object {}, and inside the function this has no specified execution context, so it refers to the global object - (which has access to the global scope of the anonymous function execution context)

Summarize

This is the end of this article about the differences between the this keyword in NodeJS and the browser. For more information about the this keyword in NodeJS and the browser, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to use nodejs to implement command line games
  • Nodejs realizes the sharing of small games with multiple people moving the mouse online at the same time
  • Implementing a multiplayer game server engine using Node.js
  • Node.js real-time multiplayer game framework
  • Is node.js suitable for game backend development?
  • A complete example of implementing a timed crawler with Nodejs
  • The core process of nodejs processing tcp connection
  • How to write a Node.JS version of a game

<<:  MySQL database must know sql statements (enhanced version)

>>:  How to deploy HTTPS for free on Tencent Cloud

Recommend

CentOS 8 custom directory installation nginx (tutorial details)

1. Install tools and libraries # PCRE is a Perl l...

Example of creating a virtual host based on Apache port

apache: create virtual host based on port Take cr...

MySQL 8.0.12 installation steps and basic usage tutorial under Windows

This article shares the installation steps and us...

js implements shopping cart addition and subtraction and price calculation

This article example shares the specific code of ...

ffmpeg Chinese parameter description and usage examples

1. When ffmpeg pushes video files, the encoding f...

MySQL view principles and basic operation examples

This article uses examples to illustrate the prin...

Docker-compose installation db2 database operation

It is troublesome to install the db2 database dir...

Commonplace talk about MySQL event scheduler (must read)

Overview MySQL also has its own event scheduler, ...

A brief analysis of the knowledge points of exporting and importing MySQL data

Often, we may need to export local database data ...

Detailed description of component-based front-end development process

Background <br />Students who work on the fr...

JavaScript to achieve window display effect

This article shares the specific code of JavaScri...

Thoughts on copy_{to, from}_user() in the Linux kernel

Table of contents 1. What is copy_{to,from}_user(...

Summary of Linux file directory management commands

touch Command It has two functions: one is to upd...

Summary of Operator Operations That Are Very Error-Prone in JavaScript

Table of contents Arithmetic operators Abnormal s...