This keyword in JavaScript

Source: Internet
Author: User

The this keyword in js is different from this in other object-oriented languages.

The following describes the differences between this keyword in java and the memory analysis of this keyword in js.


I. Memory Analysis
1. this keyword in java
This refers to the current object (reference of the object itself)

Why does this exist in the heap?

This keyword is used inside a method to indicate reference to the object that calls the method. So since this method can only be used inside the method, it represents a local meaning, which is equivalent to a local variable. Local variables are stored in the stack in the memory. When the method is executed, this disappears! However, unlike local variables, this exists in each method in the same class! This is equivalent to an attribute of an object. It always exists no matter whether we use it or not! This is also the reason why this exists in the heap!


2. this keyword in Js

The keyword "this" indicates the object that calls this method. It is used in the method of the object. You can use this to reuse the same function in any place.

<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + ICAgICAgICAgICA8aW1nIHNyYz0 = "http://www.bkjia.com/uploadfile/Collfiles/20140116/20140116092130194.jpg" alt = "\">

As shown in: this is a keyword of the Javascript language. It represents an internal object automatically generated when the function is running and can only be used inside the function.

After comparison with the above, we can draw a conclusion that this in js depends on the method. Only when the method is called can this function be used to determine who points to it! In java, this can be seen as an object's own property independent of methods. As long as the object exists, this exists.


Ii. Use of this in Js

In Js, this always points to the object that calls this method, so we can analyze the usage of this from the method as the starting point.


1. Define the function in window using the this keyword



There are two methods to call this Method. Both of them can find the function-Method (famous Method) in the corresponding memory and execute it. However, the reason for different execution results is: who calls the method, then this indicates who.
One is direct call.

The caller of Method () is window.

People who have some understanding of window should know that the Javascript page-level scope is window, that is, the default famous object is equivalent to an attribute of window, such as defining var a; it is equivalent to specifying window. attribute a, defining function B is also equivalent to specifying window. B attribute. Therefore, the call of Method () is equivalent to the call of window. Method (), and the caller is window, so this = window returns true;


One is to call through an object

Obj. Method () the caller is Obj, so this = Obj returns true;


2. Define a public attribute in an object and use the this keyword in the method.


There is only one call method for this definition method, called through an object;

However, the getP function is defined only once and runs on different occasions to display different running results. this is determined by the change of the this pointer.

This processing mechanism in Js: this indicates who calls the method.

In the getP method of obj1, if obj1 calls this method, this points to the obj1 object;

In the getP method of obj2, if obj2 calls this method, this points to the obj2 object, and uses this pointer to reference the property p of both objects.

By the nature of this pointer, you can better understand the nature of objects in JavaScript: an object is a set of one or more attributes (methods. Each set element can not only belong to one set, but dynamically belongs to multiple sets. In this way, the this pointer directs to the person who calls a method (set element.


3. Use the this keyword in the constructor.



Make a modification to indicate that this is not a global object in the constructor.





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.