JS's Fat arrow problem

Source: Internet
Author: User

When we declare a function, we are usually

var foo function (a) {

Console.log (a)

};

With ES6, we write this.

var foo = a =>{

Console.log (a);

};

Foo (2);

It's just less of a function field what's the redundancy thing?

First look at the following code:

var obj={

ID: "Love"

Love:functionlove () {

Console.log (this.id);

}

};

var id = "hit"

Obj.love (); Love

SetTimeout (obj.love,100); Love

All love, nothing wrong, it should be settimeout (obj.love,100); Enter hit

According to the analysis, the love function loses the binding between the same and this, should be hit this should be the window most commonly used is the var self = This, to handle the following this:

var obj = {
ID: "Not Love",
Love:function () {
var = this
Console.log (this.id);
}};
var id = "Not hit"
Obj.love ()
SetTimeout (Obj.love (), 1000);

Oh, the legendary fat-tipped. This binding behavior is completely inconsistent with normal function behavior, is it solved in es5? No, in strict mode, these are not problems, but in non-strict mode it will output hit

JS's Fat arrow problem

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.