Arrow functions in the ES6

Source: Internet
Author: User

To tell you the truth, since the arrow function has been out, there is no need to worry about the this problem, and on the one hand, to simplify the code, the arrow function is loaded force artifact.

There are a few points to note in the arrow function, if the return value has only one row of expressions, you can omit return, which means that the row is the return value, otherwise you need to add a curly brace and return. If you have only one parameter, you can omit parentheses, and two need parentheses. For example, the following:

varF = v = v*2;//equivalent tovarf =function(v) {returnV*2;}//Judging even numbersvarIsEven = n = n% 2 = = 0;//need to add returnvar= (A, b) = = {  if(A >=b)returnA; returnb;}

The This of the normal function is mutable, we classify the function into two states, one is the definition, the other is execution, and if careful study finds that the this in the function is always the object where the function executes. For example, when the global function executes, this executes the window, the object's method executes, and this is the variable of the function. And the This in the arrow function is fixed, see the following example:

function obj () {  setTimeout (()=>console.log (this. id);} var id = 1//  2

The result of the execution is 2 instead of the global 1, indicating that when the SetTimeout function executes, this point is not the window, which differs from the normal function.

In fact, the arrow function does not have this object, and turning the arrow function into ES5 will find:

// ES6 function obj () {  setTimeout (()=>console.log (this. id);} // ES5 function foo () {  varthis;  SetTimeout (function  () {    Console.log (this. ID);   );}

Methods such as Call aply cannot bind the This in the arrow function:

var  This . x; var x = 1//  1

A summary of this is to use the arrow function directly in the object's method, pointing to the window, the other arrow functions this will point to the previous layer of this, and the arrow function does not store this:

var obj = {  1,  foo: ()==    {returnthis  . Id;}  } var id = 2//  2

In addition to this, the arguments of the arrow function does not exist, cannot be constructed using new, and cannot use the yield command.

Arrow functions in the ES6

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.