Problem with this keyword in Nodejs

Source: Internet
Author: User

Before you analyze specific content, you must read the following two blog posts

Learn JavaScript closures (closure)

This usage of javascript

These two articles are a summary of the closure and this usage of the JavaScript by Ruan Feng, a teacher.

In summary, closures can be roughly understood as the binding of the execution function to the variables of the environment context. This is the environment object that points to the calling function.


In the second chapter, the use of this is an analysis of the relative browser. Many students in the comments have pointed out that this keyword in Nodejs is not a property of the text.

After the experiment, I found that it was true as commented.


(1) First, for the second section of teacher Ruan, the 4th code, in the Nodejs fragment as follows:

var x = 1;function Test () {  this.x = 0;} Test (); Console.log (x); 1 rather than 0
In teacher Ruan's browser scripting environment, Mr. Ruan said that the return is 0, which means the current Window object is pointing to this.

In Nodejs, when test () is called, the Global X of the execution module where this code fragment resides is set to 1, and the x that is output by the console is still the x declared by Var. can be verified by modifying the above code.

x = 1;function Test () {  this.x = 0;} Console.log (x); Returns 1test (); Console.log (x); Returns 0

(2) Analysis of the closure of the last chapter of the teacher Ruan left to everyone to analyze the code

First, post the code:

var object = {  Name: "My object",  getnamefunc:function () {    console.log (this.name);    return function () {      return this.name;    };}  }; Console.log (Object.getnamefunc ());

The resulting knots are my object and undefined, respectively. At this time, it is estimated that many students can not understand that the previous output is from the use of closures, the back of the function returned in the exactly where the this object from? Should this not refer to the environment object that called the function? What should that environment correspond to?

In fact, the code changes slightly, you will get a preliminary answer.

Put the first line of code var name = "Local"; Change to name = "global";

We'll find out that the second result has an output, global. I believe some students have the answer in mind, we verify the real reason. Re-transform the code:

var object = {  Name: "My object",  getnamefunc:function () {    return function () {      return this.name;    } ;  }}; name = "Global"; Console.log (Object.getnamefunc ()); var obj = {  name: "Last Result",  Myf:object.getNameFunc () };console.log (OBJ.MYF ());
After the Nodejs is run, the output global and last result are respectively obtained.

The answer is already very clear. This keyword is useful when it is actually executed. The This object still refers to the object that executes the function when the function is executed.

When I assign a value of Object.getnamefunc () to MYF, it is actually equivalent to the function functions () {return this.name;} Assigned to MYF, when MYF is called, this is determined by execution to point to the object obj.


So we conclude that:

The students were all fooled by the teacher Ruan!!! But the closure and this in JavaScript are key knowledge that needs to be fully mastered, otherwise it can be error-prone.

Problem with this keyword in Nodejs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.