ES6 Notes ② Arrow functions

Source: Internet
Author: User

Feature Introduction

The arrow function is one of the new features of ES6, which provides a new syntax for writing functions for the language JS.

function Fun (x, y) {return x+ y;}                Console.log (Fun (5,10)); // 15

// ES6 var fun6 = (x, y) = = {return x+y;} Console.log (Fun6 (6,10)); // -

ES6 simplifies the declaration function and does not need to be written again function .

It seems that there is nothing strange about it except simplifying the code, so let's look at it slowly.

——————————————————————————————————————————————————————————

The arrow function supports two modes of function body notation, let's call him concise function body and block-level function body.

// Concise function Body var fn = x + x * x; // block-level function body var fn = (x, y) = = {return x + y;};

Introduction function Body By default, the result of an expression is returned, and the block-level function body needs to be manually return .

This point

Functions generated by function define one of their own, and the this arrow function does not have its own this , but it is shared with the scope of the previous layer this .

ES5, the inner layer function if you want to use the This property of the outer function, you must first assign this to the self variable and then take the value through self.
function Person () { this. A = ten; var This ; return function () { return self.a++; } } var closure = person (); Console.log (Closure ());//10 Console.log (Closure ());//11

If you use the arrow function is much more convenient, the code is as follows:

will be shared with the previous level of the scope this //One more set of layers
functionPerson () {//function person () { This. A = 10; THIS.A = 10; return() ={return () = {return This. a++; Return () =>{} return this.a++; }}varClosure =Person (); } console.log (Closure ());//Ten}Console.log (Closure ());//One -to-one var closure = person ();
Console.log (Closure ());//10
Console.log (Closure ());//11
Apply & Call

Because the arrow function has already bound this the value, even if used apply or can call not, can only play the role of parameters, and can not forcibly change the arrow function this .

varAdder ={x:1, Add1:function(y) {varfn = v + V + This. x; returnfn (y); }, Add2:function(y) {varfn = v + V + This. x; varwhatever ={x:2              }; returnFn.call (whatever, y); //this is not whatever
} };
Console.log (ADDER.ADD1 (// 2
// One

PS: Thispoints to the question JS this is more bizarre, who executes on the point who

Arguments
var fn = (... rest) = rest[2];       Console.log (FN (2,0,5));  // 5
...restis also ES6 a new feature that will be introduced later.
We know that there is a arguments object inside the JS function that can get all the arguments. Now ES6 has brought us a new object that can get the parameters except the start parameter, i.e. the remaining parameters
This new object is not the same as the arguments, it is a programmer custom of a common identifier, just need to add three points in front: ...

The remaining parameters in the ES6 function are the original address: http://www.cnblogs.com/snandy/p/4482463.html

Cannot be new

Arrow functions cannot be new used with keywords, error

var Fn = () = {           this. A = 1;    };  var New Fn ();    // Error

ES6 Notes ② Arrow functions

Related Article

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.