This in simple JavaScript

Source: Internet
Author: User
Keywords JavaScript

This article only picks one example: http://www.aliyun.com/zixun/aggregation/33906.html ">javascript" The This keyword, in simple and simple analysis its meaning in different situations, The reason for this and the method of binding this is provided in JavaScript tools such as Dojo. It can be said that the right to grasp the JavaScript in the This keyword, only to enter the language of JavaScript threshold.

In object-oriented languages such as Java, the This keyword has a clear and specific meaning, referring to the current object. is generally determined at compile time, or is called compile-time binding. In JavaScript, this is a dynamic binding, or runtime binding, which causes the This keyword in JavaScript to have multiple meanings, which brings flexibility and confusion for beginners. This article only discusses this issue, read the article, if the reader can correctly answer the What ' This question in JavaScript, as the author, I will be 211.html "> think it is worth the effort to write such an article."

This in the Java language

Defining classes in Java often uses the This keyword, in most cases, to avoid naming conflicts, such as defining a point class in the example below, it is natural that you use X,y to name its attributes or member variables, and in the constructor, use X,y to name the arguments, compared to other names, such as A,b, also more meaningful. This is the time to use this to avoid naming conflicts. Another scenario is to make it easy to invoke other constructors, such as a point defined on the x-axis, whose x value defaults to 0, and when used, we can define a constructor that simply passes in one parameter. In either case, this means the same thing, meaning the current object.

Listing 1. Point.java

public class Point {private int x = 0; private int y = 0; Public point (x, y) {this.x = x; this.y = y;} (0, y); } }

This in the JavaScript language

Because of its run-time binding attributes, the This meaning in JavaScript is much richer, it can be a global object, the current object, or any object, depending entirely on how the function is invoked. There are several ways to invoke functions in JavaScript: As an object method call, as a function call, as a constructor call, and with an apply or call invocation. Below we will discuss the meaning of this by different ways of calling.

Called as an object method

In JavaScript, a function is also an object, so a function can be a property of an object, at which point the function is called the method of the object, which is naturally bound to the object when the invocation is used.

Listing 2. Point.js

var point = {x:0, y:0, moveto:function (x, y) {this.x = this.x + x; this.y = This.y + y;}}; Point.moveto (1, 1)//this bound to the current object, the Point object

As a function call

A function can also be called directly, at which point this binds to the global object. In the browser, window is the global object. For example, the following example: When a function is called, this is bound to the global object, and then the assignment statement, which is tantamount to declaring a global variable implicitly, is clearly not what the caller wants.

Listing 3. Nonsense.js

function Makenosense (x) {this.x = x;} makenosense (5); x;//x has become a global variable with a value of 5

For an intrinsic function, a function declared in the body of another function, this way of binding to the global object creates another problem. We still take the point object mentioned above as an example, this time we want to define two functions within the MoveTo method, and translate the x,y coordinates separately. The result may be unexpected, not only the point object does not move, but more than two global variable x,y.

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.