Talk about my understanding of this object in JavaScript

Source: Internet
Author: User

implicit assignment of this pointer

This always points to the object that called the method;

In the event, This points to the object that triggered the event, and, in particular, the this in theattachevent in IE always points to the Global object Window;

show manipulating this pointer

The JavaScript engine allows us to explicitly specify the object that this pointer refers to in the following two ways:

1. With the new operator, the JavaScript engine returns the this pointer to the assigned variable.

2. By means of function.apply or function.call or function.bind, we can refer to the object of this pointer as an argument, at which point the this pointer used inside the function is the passed in parameter.

the This object in the closure

The This object is bound at run time based on the execution environment of the function: in the global function, this equals window, and when the function is called as a method of an object, this is equal to that object. However, the execution environment of an anonymous function is global , so its this object usually points to window. But sometimes this may not be so obvious because of the way closures are written. Let's look at an example:

 var  name = "The window" ;  var  obj = {name:  "My obj"  function   () {  return  function   () { return  this  .name;  }; }}alert (Obj.getnamefunc ());  //  "the Window" (in non-strict mode)  

The above code first creates a global variable name and creates an object that contains the Name property. This object also contains a method--getnamefunc (), which returns an anonymous function, while

var name = "the window"; var obj = {  "My obj",  function() {     var   This ;      return function () {       return   that.name;    };   //

In several special cases, the value of this may change unexpectedly. For example, the following code is the result of modifying the previous example:

var name = "the window"; var obj = {  "My obj",  function() {     return  this. Name;   // ////

The third line of code executes an assignment statement before calling the result after the assignment. Because the value of this assignment expression is the function itself, the value of this cannot be maintained (no reference to obj, the currently active object is window), and the result returns "the window".

Talk about my understanding of this object in JavaScript

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.