The This in JavaScript

Source: Internet
Author: User

In JavaScript, this is a keyword, not a variable, or a property name. The syntax of JavaScript is not allowed to assign a value to this.

Note: This does not have scope restrictions, and nested functions do not inherit this from the function that calls it (this inside the function does not inherit the scope of the external this). if the nested function is a method call (O.F ()), its this value points to the object that called it, and if the nested function is called as a function , its this value is a global object (non-strict mode) or undefined (strict mode).

Many people mistakenly assume that this will point to the context of the calling outer function when the nested function is called. If you want to access the this value of an external function, you need to save the value of this in a variable (such as self), which is within the same scope as the intrinsic function. Like what:

varo={m:function(){       varself= This; Console.log ( This= = = O);//true, this is the object of (); function() {Console.log ( This= = = O);//false, the value of this is a global object or undefinedConsole.log ( This= = = O);//true, self refers to the value of the external function this}}};o.m ();

The This 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.