Arrow function-------JavaScript

Source: Internet
Author: User

This article abstract: http://www.liaoxuefeng.com/

The ES6 standard adds a new function:arrow function.

Why is it called Arrow Function? Because it's defined by an arrow:

x => x * x

The above arrow functions are equivalent to:

function (x) {    return x * x;}

The arrow function is equivalent to an anonymous function and simplifies the function definition. The arrow functions have two formats , one like the above, containing only one expression , { ... } and return both are omitted. There is one more statement that can contain multiple statements, and it is not possible to omit { ... } and return :

x => {    if (x > 0) {        return x * x;    }    else {        return - x * x; }}

If the arguments are not one, you need to () enclose them in parentheses :

// 两个参数:(x, y) => x * x + y * y// 无参数:() => 3.14// 可变参数:(x, y, ...rest) => {    var i, sum = x + y; for (i=0; i<rest.length; i++) { sum += rest[i]; } return sum;}

If you want to return an object , be aware that if it is a single expression, you will get an error if you write this:

// SyntaxError:x => { foo: x }

Because { ... } there is a syntax conflict with the function body, change to:

// ok:x => ({ foo: x })
This

The arrow function appears to be a shorthand for anonymous functions, but in fact, there is an obvious difference between an arrow function and an anonymous function: the lexical scope inside the arrow function this is determined by the context .

Looking back at the previous example, this The following example does not get the expected result due to the JavaScript function's error handling of the binding:

var obj = {    birth: 1990,    getAge: function () { var b = this.birth; // 1990 var fn = function () { return new Date().getFullYear() - this.birth; // this指向window或undefined }; return fn(); }};

Now, the arrow function completely fixes this the point, this always pointing to the lexical scope , that is, the outer caller obj :

var obj = {    birth: 1990,    getAge: function () { var b = this.birth; // 1990 var fn = () => new Date().getFullYear() - this.birth; // this指向obj对象 return fn(); }};obj.getAge(); // 25

If you use the arrow function, the previous type of hack notation:

var that = this;

It's no longer necessary.

Because the this lexical scope has been bound in the arrow function, it is call() apply() not possible to bind when using or invoking an arrow function, that is, this the first parameter passed in is ignored:

var obj = {    birth: 1990,    getAge: function (year) { var b = this.birth; // 1990 var fn = (y) => y - this.birth; // this.birth仍是1990 return fn.call({birth:2000}, year); }};obj.getAge(2015); // 25

Arrow function-------JavaScript

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.