This in javascript

Source: Internet
Author: User
What is this in js? There are too many explanations for this in the blog garden. However, after reading this, I feel that the explanation of this is a bit complicated. Therefore, I hereby give this a simple and easy-to-understand definition. This is actually an object of js. So what is the object? It is very simple. The object "this" is: who calls the callback and it points to who. In fact, there are many articles in the blog park. What is this in some js? There are too many explanations for this in the blog garden. However, after reading this, I feel that the explanation of this is a bit complicated. Therefore, I hereby give this a simple and easy-to-understand definition.

This is actually an object of js. So what is the object? Very simple, This object is: who calls callback, and who it points.

In fact, there have been a lot of articles in the blog Park, and some articles have also mentioned ideas, but they still have a vague explanation. Here, I will give you a few simple comparative experiments. According to the results of these comparative experiments, you should be very clear.

I hope you can repeat the following experiment based on my code.

First, let's look at the following code:

// Create a local object avar a = {user: 'Little East', getName: function () {return this. user; // return the current object's user property value }}// call the getName method alert (. getName ());



What will the above Code output? That's right. Here, alert is called internally in object a. It must be called by object, therefore, according to zookeeper who calls this, it points to who defined zookeeper who calls this ..........., this points to object.

Next, we will add a line to the above Code:

// Create a local object avar a = {user: 'Little East', getName: function () {return this. user; // return the current object's user property value }}// call the getName method alert (. getName (); // assign the attribute function of a local object to the variable outvar out =. getName; // call the out function alert (out ());



Based on the above experiment results, we know that Xiaodong is output by calling a. getName directly, right? So, after we assign the attribute function a. getName to the out variable, what will be output when we call out? The result is undefined. Why is it undefined? Think about it for a while. If you understand why undefined is output here, I think your understanding of this is clear. However, if you are interested in this article, you can continue to read it.

Here, I will not explain why the getName function of object a cannot get the value in object a when it is referenced outside, please refer to the following code (in fact, it is similar to the above Code, haha ):

// Create a local object avar a = {user: 'Little East', getName: function () {return 1; // here we do not return this, returns 1} // calls the getName method alert (. getName (); // assign the attribute function of a local object to the variable outvar out =. getName; // call the out function alert (out ());



The above code is very simple. Here, we don't return the things of object a in the getName attribute function of object a. Instead, we return the rows between 1 rows without rows between rows, now you call the out function. What do you think will be output? That's right, what you get is no longer just a real number without being defined ....... That's strange, right? When. when the getName function returns the user attribute in object a, we use out to reference. the getName function returns the undefined result. when the getName function returns 1, we use out to reference. the getName function can obtain. what's in getName? What's going on?

The reason is very simple. When we call the global variable out, this points to the out object instead of the object. Who is the out object? We should know that the object of the variable declared by js in the global environment is the window object.Since this points to window when calling out, the function out =. getName = function () {return this. this. user we should write the window. user, right?However, is there a window. user attribute value in the global variable? No, right? Since there is no attribute value for window. user, alert (window. user) must be undefined. To prove this, we will do the following experiment:

// Create a local object avar a = {user: 'Little East', getName: function () {return this. user; // return the current object's user property value }}// call the getName method alert (. getName (); // assign the attribute function of a local object to the variable outvar out =. getName; // we add a window. global attribute of the user to see what var window will be output when the out function is called again. user = 'window "s username'; // call the out function alert (out ());



At this moment, when you execute the above Code, you will find that the output of the out function is not undefined, but window's username. What does this prove? It proves that when the out calls this, this actually points to the window, and also proves that this is indeed: Who calls callback, it points to who.

If you do not understand the global variables of js, we can replace the above Code with the following code and then call out, so that you can better understand:

// Create a local object avar a = {user: 'Little East', getName: function () {return this. user; // return the current object's user property value }}// call the getName method alert (. getName (); // assign the attribute function of a local object to the variable outvar out =. getName; // at this moment, we add a user variable to see what var user = 'window "s username' will be output when the out function is called again '; // call the out function alert (out ());



Here, we no longer enable the window object. We use var to declare all the attributes and functions outside of the window. Since var is used to declare them, should they all point to the same object? Now, you can execute the above Code, that is, execute the out function. You can still use alert to output window's username. At this point, the definition of this can no longer be understood. It is: who calls callback, it points to who.

This is not hard to understand. What is hard to understand is that you need to find the object that calls this, which has been called, and has been called, and only the object that calls this is found, only then do you know who this actually points to, because the definition of this is: who calls this will point to who it points.


The above describes the details of this in javascript. For more information, see other related articles in the first PHP community!

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.