The wall grass in JS---this

Source: Internet
Author: User

In Java, this is always "faithful" to an instance of the current class. In JavaScript, this is not "loyal," and this changes as the execution environment changes, which means that this always executes the object where the execution environment resides.

Directly on the code:

Code One:

function test () {alert (this = window)}test ()

The result is found to be true by running code, because the test method is called when in the window environment, so this is pointing to window.


Then look at:

Use object literals to define properties and methods, as follows:

var obj = {property: ' obj ', fun:function () {alert (this = = window);    Alert (This = = obj); }}obj.fun ();

Run the code and the result is false,true. What is the reason for pinching? The reason is simply that the fun method is called through the Obj object, so this points to the Obj object.

What's the problem? Wood has, ' (*∩_∩*). Let's Go!

We know that you can also return a new method in a method, in the following form:

 var obj = {    property :  "obj",     fun  : function () {        return function () {             alert (this == obj);             alert (This == window);         }    }}obj.fun ()   We changed the code above slightly, and returned a method in the fun method. What's the result? Have you guessed the result? The correct result is false,true. Let's analyze that using  obj.fun () returns a method that is in the Global Environment (window), so this points to window. You got it.                                             Here you are not to JS in the this has a general understanding of, a little, there is a situation, is through the call and apply method to dynamically change the point of this. We'll just show you an example of call here. Function test () {    alert (This == window)      alert (this == obj);} Var obj = {};test.call (obj,null); We continue to use the first example. We have defined an empty object, obj, and our intention is to execute the test method as a method of obj. You can take a look at the results and see if the results are inconsistent with the results of the first example                      Well, today, for the time being introduced here, typesetting for the time being asked to change, do not affect the results. If you have any questions, please leave a message.

This article is from the "Eason's HCC" blog, so be sure to keep this source http://hcc0926.blog.51cto.com/172833/1562179

The wall grass in JS---this

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.