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
a : Indicates the starting or destination positio...
Table of contents 01 Container consistency 02 Con...
The smallest scheduling unit in k8s --- pod In th...
To do a paginated query: 1. For MySQL, it is not ...
Recently, I need to package the project for membe...
When I was printing for a client recently, he aske...
This article example shares the specific code of ...
Ⅰ. Problem description: Use html+css to implement...
Table of contents 1. Location Object 1. URL 2. Pr...
Table of contents Preface introduce 1. Mechanism ...
1.ssh command In Linux, you can log in to another...
After spending half the night on it, I finally ma...
1. Custom text selection ::selection { background...
We all know that the performance of applications ...
Install ssh tool 1. Open the terminal and type th...