This is detailed in JavaScript

Source: Internet
Author: User

This is a special keyword that is automatically defined in the scope of the function. Actually learning this reminds me of the time when I studied attributive clauses.

1. Why Use this

You can implicitly pass a reference to an object

2. When a function is called, a new execution context is created and the execution context is divided into two stages.

Create phase: Create variable object, establish scope chain, determine this point

So this depends on the execution context, which is how the function is called. Confirm this point to find the function call location.

3. Binding rules

Find the call location and determine which of the four rules.

(1). Default bindings

You can see this pointing to the global variable, window.foo ();

Strict mode, unable to bind by default, can cause typeerror.

(2). Implicit binding considers whether the call location has a context object, or whether it is contained or owned by an object

Obj contains foo,this point to obj, and only the last layer affects the call location.

***

Implicit binding loses an object

The back function modifies this

(3). Explicit binding

Call ()

Apply ()

Foo.call (obj) hard bind, no loss of bound object

(4) New

NEW: Create an object, change this point, connect to the prototype chain, bind the method property to this, and return this

This is detailed in JavaScript

Related Article

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.