JavaScript this-notes

Source: Internet
Author: User

Reference article: blog.crimx.com/2016/05/12/understanding-this/

Notes:
Before the ES6 arrow function This is determined at the time of execution, not when it is defined.
function is called, call when looking for the previous call object, there is a clear display is explicitly displayed object, not remember to look at hidden objects
Functions are also objects, functions can act as properties of an object (this is called a method)
When called as a function, this binds to the global object,
When called as a method, this binds to the object to which the method belongs

1.this in non-strict mode, is the window
In strict mode is undefined
2. The function (or method) in the call, is always the object in the call,
Obvious format, Obj.fun () this is the obj
Stealth format, fun () at this time this is the window, in fact, so Window.fun (), in JS window directly hidden do not write
3. Change This,call and apply with no return value via call and apply
Refer to the "Ye" law (the method of a object to the B object, a object is ye, to write in front), Format Object.prototype.call ()
At this point this is the call/apply first parameter
Call is the same as the Apply function, except that the second parameter is not the same as the form parameter, and the Apply parameter is an array form
4.bind after changing this, the call and apply do not take effect, and bind returns the function that called bind
Bind execution time this point is also the first parameter
5.new a ' class ' (function in JS), this binds to the new object
function Test () {
Console.log (This)
}
The new Test ()//test{} is bound to an empty object first
6. Arrow functions
Where to create this point to WHO

JavaScript this-notes

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.