This summary in JavaScript

Source: Internet
Author: User

1. About this
We need to determine the binding rules used in this function based on the "calling method" of the function on the "Call location"
2. Binding rules
(1) Default binding: This==>winow Object
(2) Implicit binding
The context object referenced by the 1.this==> function
2. Implicit loss: There is an implicit loss when a reference pass occurs: reference assignment, parameter transfer;
3. Implicit loss Solution: using hard binding (a form of displaying bindings)
(3) Explicit binding
This==> the object we specified
(4) New binding
This==> Instance Object
3. Priority level
New binding > Show Bindings > Implicit binding > Default Bindings

4. Rules under normal circumstances
(1) is the function called in new (new binding)? If so, this binds to the newly created object.
Varbar=newfoo ()
(2) is the function called by call, apply (explicit binding), or hard bind? If so, this binds to the specified object.
Varbar=foo.call (OBJ2)
(3) is the function called (implicitly bound) in a context object? If so, this binds to the context object.
Varbar=obj1.foo ()
(4) If not, use the default binding. If in strict mode, bind to undefined, otherwise bind to the global object.
Varbar=foo ()

5. Binding Exceptions
(1) In some scenarios, the binding behavior of this will be unexpected, you think you should apply the other binding rules, you may actually apply the default binding rules

(2) Soft binding: The arrow function This does not conform to our this normal binding rule

6, currying
Simply pass to the function part of the argument to invoke it, and let it return a function to handle the remaining arguments. (pre-Define some parameters)




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