The this binding of the ECMASCRIPT6 lambda (arrow function) causes Call/apply to fail

Source: Internet
Author: User

Error Scenario:

APP = React.createclass ({    = =    {return  (      <div></div>     );  }});

In accordance with the above-mentioned wording, in the use of the app where there will be undefined problems.

There is no problem with the following notation:

APP = React.createclass ({  function  () {    return  (      (<div></div>    );  }});

Alternatively, you can use the simplified method of ES6 to define the wording:

APP = React.createclass ({  render () {    return  (      <div></div>     );  }});

Generally speaking, the place that can use lambda certainly as far as possible, concise and beautiful, but the above first kind of writing caused the error, I guess this is the problem, check found lambda and call/apply a little conflict, lambda because automatically bind this, will cause call/ Failed attempt to bind this: Arrow functions-javascript | MDN. In this case, the Lambda's this is bound to the global object: an answer to StackOverflow

I level limit, did not go to read the source of react confirmed that render is binding this use, but the reason for the above error is only this possible:)

The this binding of the ECMASCRIPT6 lambda (arrow function) causes Call/apply to fail

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.