PrefaceAnyone 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 debuggingFrom 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 analysisSo let's see why this is happening. First we need to understand how nodeJs works
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) SummarizeThis 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:
|
<<: MySQL database must know sql statements (enhanced version)
>>: How to deploy HTTPS for free on Tencent Cloud
1. Install tools and libraries # PCRE is a Perl l...
apache: create virtual host based on port Take cr...
This article shares the installation steps and us...
This article example shares the specific code of ...
1. When ffmpeg pushes video files, the encoding f...
This article uses examples to illustrate the prin...
It is troublesome to install the db2 database dir...
Overview MySQL also has its own event scheduler, ...
Often, we may need to export local database data ...
Background <br />Students who work on the fr...
This article shares the specific code of JavaScri...
Table of contents 1. What is copy_{to,from}_user(...
First, let's explain the network setting mode...
touch Command It has two functions: one is to upd...
Table of contents Arithmetic operators Abnormal s...