JavaScript fully understands the scope (basic, this, closure, inheritance) 2

Source: Internet
Author: User

This is something you really don't understand. It's very confusing. It's wrong if you're not careful.

This indicates the current scope. If you know the current scope, you can determine it, but this is not easy to determine.

Part 4: Use this


<Script>
Var a = 2;
Function test (){

Var a = 1;
Document. writeln ();
Document. writeln (this. );

}

Test ();
Document. write (this. );
</Script>
<Script>
Var a = 2;
Function test (){

Var a = 1;
Document. writeln ();
Document. writeln (this. );

}

Test ();
Document. write (this. );
</Script>
What do you think is the result? 1 1 2? But the result is 1 2 2.

Is there any doubt? This function is called in window, so this is window, window. a = 2. If this does not exist in test (), this is in the test field.

Part 5: using this in an object


<Script>
Var name = "JIM ";
Function Person (){
This. name = "Mike ";
This. myname = getName;
}

Function getName (){
Return this. name;
}

Var person = new Person ();
Document. write (person. myname ());
</Script>
<Script>
Var name = "JIM ";
Function Person (){
This. name = "Mike ";
This. myname = getName;
}

Function getName (){
Return this. name;
}

Var person = new Person ();
Document. write (person. myname ());
</Script>
The result is Mike. The scope of this is in person. First, search for the attributes, then search for the prototype, and find them all the time.


<Script>
Var name = "JIM ";
Function Person (){
This. myname = getName;
}
Person. prototype. name = "Mike ";
Function getName (){
Return this. name;
}

Var person = new Person ();
Document. write (person. myname ());
</Script>
<Script>
Var name = "JIM ";
Function Person (){
This. myname = getName;
}
Person. prototype. name = "Mike ";
Function getName (){
Return this. name;
}

Var person = new Person ();
Document. write (person. myname ());
</Script>
The Person. prototype. name field is found all the time.

Part 5: this is used in inheritance


<Script>
Var name = "JIM ";
Function Person (){
This. myname = getName;
This. name = "Mike ";
}
Function Son ()
{
This. name = "Sam ";
}
Son. prototype = new Person ();
Function getName (){
Return this. name;
}

Var son = new Son ();
Document. write (son. myname ());
</Script>
<Script>
Var name = "JIM ";
Function Person (){
This. myname = getName;
This. name = "Mike ";
}
Function Son ()
{
This. name = "Sam ";
}
Son. prototype = new Person ();
Function getName (){
Return this. name;
}

Var son = new Son ();
Document. write (son. myname ());
</Script>
The result is Sam. The caller is son. He first looks for his own attributes, then the prototype, and finally the prototype.

Part 6: use this in the callback function


<Script>
Var name = "JIM ";
Function Person (){
This. name = "Mike ";
}
Function getName (){
Return this. name;
}
Var person = new Person ();
Document. writeln (getName. call (person ));
Document. write (getName. call (this ));
</Script>
<Script>
Var name = "JIM ";
Function Person (){
This. name = "Mike ";
}
Function getName (){
Return this. name;
}
Var person = new Person ();
Document. writeln (getName. call (person ));
Document. write (getName. call (this ));
</Script>

The result is Mike JIM. The first one is equivalent to the person being called and searching for his scope. The second is that the window is called and the window scope is searched.

Part 7: using this in closures

The following example may be your favorite. Why?


<Script>
Var name = "JIM ";
Function Person (){
This. name = "Mike ";
This. getName = function (){
Return function (){
Return this. name;
};
};
}

Var person = new Person ();
Document. write (person. getName ()());
</Script>
<Script>
Var name = "JIM ";
Function Person (){
This. name = "Mike ";
This. getName = function (){
Return function (){
Return this. name;
};
};
}

Var person = new Person ();
Document. write (person. getName ()());
</Script>
You thought it was Mike, but you were wrong. The result was JIM.

Let me analyze it. This will not be saved in the scope chain. Let's split person. getName.


Var temp = person. getName ();
Document. write (temp ());
Var temp = person. getName ();
Document. write (temp ());
Person. getName () returns a function. It can save other scopes. this is not saved. The first step is to return a function. What is the result of getName, the location of the call. Here is window, of course, JIM. Www.2cto.com

We can summarize the usage of this in the function. If someone calls him, this directs to him and searches for his scope.

Now, we can fully understand how this is used and who it points.

From WebGIS, step by step

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.