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