The arrow function in ES6

Source: Internet
Author: User
Today I'm going to summarize some of the things ES6 does with functions. Combined with my ES5 function features, can let us according to the actual situation, to very good completion of our project requirements. One, ES6 arrow function

1. Arrow function with parameter

Let fun = (x) => x
console.log (Fun (2))//2

This is equivalent to the ES5

function Fun (x) {return
  x
}
Console.log (Fun (2))  //2

2. Give the parameter default value

Let fun = (x = 0,y = 0) => console.log (x+y);
Fun (3);  3
Fun (2,3)//5

This is equivalent to the ES5

function Fun (x,y) {
  var x = x | | 0;
  var y = y | | 0;
  Console.log (x+y);
}
Fun (3);  3
Fun (2,3)//5

is not a lot easier.

3. Parameter As Object

Let fun = ({name,age}) => name+ ' is ' +age+ ' years old ';
Let obj = {
    name: ' King ', age
    : ' Fun '
}
console.log (obj)//king is-years old

This is equivalent to the ES5

function Fun (obj) {return
    obj.name+ ' is ' +obj.age+ ' years old ';
 }
 Let obj = {
    name: ' King ', age
    : ' Fun '
 }
 console.log (obj)//king is-years old

4. Above we only execute a code statement in the function body, if we want to execute multiple we need to use {}

Let fun = (x = 0,y = 0) =>{
    console.log (x+y);
    Console.log (x-y);
}
Fun (2,3);

5. If we want to return the data is the object we should use (), no words {} will be considered a block of code, and be executed.

Let fun = () => ({name: "Jhon", age:23});

Console.log (Fun ())//Return object {name: "Jhon", age:23}

6. In the method of the object we can simplify the function

Let obj = {
    name: "King",
    Fun () {
          alert (1)
    }
 }
 obj.fun ();//1

This is equivalent to the ES5

Let obj = {
    name: "King",
    Fun:function () {
          alert (1)
    }
 }
 obj.fun ();//1
Second, the attention point of function in ES6

1. Arrow function can not use new to instantiate the object, if you do so, will be an error

Let fun = (x = 0,b = 0) =>{console.log (x+b);
Let Funclone = new Fun (); Error fun is not a constructor
Funclone (2,3)

2. The arguments object cannot be used, the object does not exist in the function body, and if so, it can be replaced with rest parameters;

3. The This object in the function body is the object of the definition, not the object in which it is used;

var obj = {
    name: "King",
    Fun () {
        settimeout () => {///using the arrow function
            console.log (this);
        },1000)
    },
    fuc:function () {
        settimeout (function () {
            console.log (this);
        },1000)
    }
  } Obj.fun (); Returns the Obj object
  obj.fuc ();//Return Window object

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.