The point of this in JavaScript (this point of the analysis of anonymous functions is focused)

Source: Internet
Author: User

1. The meaning of this (what is this?) ):

Http://www.ruanyifeng.com/blog/2010/04/using_this_keyword_in_javascript.html

This is a keyword in the JavaScript language.

It represents an internal object that is automatically generated when the function is run, and can only be used inside the function.

https://zhuanlan.zhihu.com/p/23804247

This is the context in which you call a function simultaneous

https://dmitripavlutin.com/gentle-explanation-of-this-in-javascript/

In JavaScript the situation are different: is the current this execution Context of a function.

To summarize: This is the context in which the function executes, and when this is used, the variables and methods of the context are called.

2, this's generation and binding time:

The This object was bound at runtime based on the context in which a function is executed.

3, this point:

In the very cases, the value of is determined by how this a function is called. It can ' t be set by assignment during execution, and it could be different each time that function is called.

Translation: In most cases, the value of this (point) is determined by how the function is called. It cannot be assigned during execution, and its value may not be the same each time it is called.

The documentation for the MDN is a good illustration of the various pointers to this: Https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this

There is no longer a description of the situation here, but the analysis of this point of the anonymous function is focused:

★ Anonymous function of this point:

JS Advanced Programming Chinese version of the sentence: "Anonymous function execution environment is global, so its this object is usually pointed to window." ”

In fact, however, the original English version did not mention the overall issue of the implementation environment.

The original text is:

Anonymous functions is not bound to a object in this context, meaning the This object points to window unless executing In strict mode (where the is undefined).

Translation:

In this context (execution environment), the anonymous function is not bound to any object, assuming that this is a point to the window (unless the context (execution environment) is executed in strict mode, and in strict mode it points to undefined)

Here is the code for the appendix, this context refers to the following code

var name = "the window";

var object = {

Name: "My Object",

Getnamefunc:function ()

{

return function ()

{

return this.name;

};

} };

Alert (Object.getnamefunc () ()); "The Window" (in Non-strict mode)

Another example analysis:

var number = 1;
var obj =

{
Number:2,
Shownumber:function ()

{
This.number = 3;
(function () {
Console.log (This.number);//1 immediate execution function is not bound to the Obj object, so point to window, output is 1
})();
Console.log (This.number); the//3 shownumber function is bound to obj when called, and This.number in the function modifies the number value of obj, so the output is 3
}
};
Obj.shownumber ();

To sum up: Determine who this is, and see where the function is bound at execution time, as long as the function is not bound to be called on the object, and this is the window.

4, the strict mode of this point:

When using the Apply () or call () methods of a function, a null or undefined value was coerced to the global object in Nonst Rict mode.

In strict mode, the this value for a function is always used as speci?ed, regardless of the value.

Translation: In normal mode, when the Apply () or call () method of a function is used, if the argument is null or the undefined,this point is coerced into a global object.

In strict mode, the point of this is determined entirely by the context in which it is placed, even if it is specified as null or the undefined,this point is the context.

1.
"Use strict";
var color = "Red";
function Displaycolor () {alert (this.color);}
Displaycolor.call (NULL);
Vm139:3 uncaught typeerror:cannot Read Property ' color ' of null
At Displaycolor (<anonymous>:3:40)
At <anonymous>:4:14

2.
var color = "Red";
function Displaycolor () {alert (this.color);}
Displaycolor.call (null);//red

In Strict mode , if this it is not defined in the context of execution, it will default to undefined .

1.

"Use strict";
var color = "Red";
function Displaycolor () {alert (this.color);}
Displaycolor ();
Vm119:3 uncaught typeerror:cannot Read Property ' color ' of undefined
At Displaycolor (<anonymous>:3:40)
At <anonymous>:4:1

2.

"Use strict";
function Displaycolor () {alert (this = = window);} False
Displaycolor ();

3.
var color = "Red";
function Displaycolor () {alert (this.color);}
Displaycolor ();//red

See the MDN documentation for additional information: Https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this

The point of this in JavaScript (this point of the analysis of anonymous functions is focused)

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.