ES6 Arrow Functions

node v0.12.18
version: 2.0.0
endpointsharetweet
Arrow functions (also known as fat arrow functions) have a shorter syntax compared to traditional function expressions and lexically bind the this value. Arrow functions are also always anonymous. Here we are using the shortest form, where the body is a simple expression and we can drop the parenthesis around the parameter since there is only one:
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // Same as function(number) { return number * 2 } var doubled = numbers.map(number => number * 2);
If we want multiple parameters, we can add the parenthesis and curly braces back:
// Same as function(runningTotal, number) { return runningTotal + number } var sum = numbers.reduce((runningTotal, number) => runningTotal + number, 0);
Finally, more complex multistatement arrow functions can use curly braces:
// Here all we're really saving is the function() heading. numbers.map(number => { if (number % 3 == 0 && number % 5 === 0) return "Fizz Buzz"; if (number % 3 === 0) return "Fizz"; if (number % 5 === 0) return "Buzz"; return number; }).join("<br/>")
Arrow functions can also make your code less complex since they'll use the "this" of the surrounding scope, instead of introducing their own. This means you can avoid having to bind in many cases:
var cart = { items: [], addItem: function(anItem) { console.log("Added " + anItem + "!"); this.items.push(anItem); }, addAll: function(newItems) { // Before we'd have to do (function(anItem) { this.addItem(anItem) }).bind(this) newItems.forEach(anItem => this.addItem(anItem)); } } cart.addAll(["pickels", "tomatoes", "cheese"])
To learn more about arrow functions, check out https://hacks.mozilla.org/2015/06/es6-in-depth-arrow-functions/ Some of these examples and explanations originally taken and modified from: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions https://github.com/lukehoban/es6features#arrows https://en.wikipedia.org/wiki/Fizz_buzz
Loading…

60 comments

  • posted 7 years ago by y6657
    5m7ujgy567
  • posted 6 years ago by sa-mu
    Muito bom
  • posted 5 years ago by 5d233f5c99af1a001affbe22
    123
  • posted 5 years ago by sujonhossainbd
    Awesome. content
  • posted 4 years ago by thejack
    Nice!
  • posted 4 years ago by mass59
    anItem
  • posted 2 years ago by yousifrunkit
    Pakages
  • posted 2 years ago by abdulaziz64329
    Kjj
  • posted a month ago by alialobidm
    Github.com
  • posted 19 days ago by xsjybldb
    1
  • posted 19 days ago by xsjybldb
    1
  • posted 19 days ago by xsjybldb
    -1 OR 2+556-556-1=0+0+0+1 --
  • posted 19 days ago by xsjybldb
    -1 OR 2+765-765-1=0+0+0+1
  • posted 19 days ago by xsjybldb
    -1' OR 2+614-614-1=0+0+0+1 --
  • posted 19 days ago by xsjybldb
    -1' OR 2+527-527-1=0+0+0+1 or 'Mr3Kb96k'='
  • posted 19 days ago by xsjybldb
    -1" OR 2+891-891-1=0+0+0+1 --
  • posted 19 days ago by xsjybldb
    if(now()=sysdate(),sleep(15),0)
  • posted 19 days ago by xsjybldb
    0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z
  • posted 19 days ago by xsjybldb
    0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z
  • posted 19 days ago by xsjybldb
    (select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/
  • posted 19 days ago by xsjybldb
    -1; waitfor delay '0:0:15' --
  • posted 19 days ago by xsjybldb
    -1); waitfor delay '0:0:15' --
  • posted 19 days ago by xsjybldb
    1 waitfor delay '0:0:15' --
  • posted 19 days ago by xsjybldb
    X7DUvldV'; waitfor delay '0:0:15' --
  • posted 19 days ago by xsjybldb
    -5 OR 509=(SELECT 509 FROM PG_SLEEP(15))--
  • posted 19 days ago by xsjybldb
    -5) OR 439=(SELECT 439 FROM PG_SLEEP(15))--
  • posted 19 days ago by xsjybldb
    -1)) OR 467=(SELECT 467 FROM PG_SLEEP(15))--
  • posted 19 days ago by xsjybldb
    jWX2b0Wb' OR 913=(SELECT 913 FROM PG_SLEEP(15))--
  • posted 19 days ago by xsjybldb
    nifm1y1w') OR 96=(SELECT 96 FROM PG_SLEEP(15))--
  • posted 19 days ago by xsjybldb
    lxNTaB3L')) OR 906=(SELECT 906 FROM PG_SLEEP(15))--
  • posted 19 days ago by xsjybldb
    1*DBMS_PIPE.RECEIVE_MESSAGE(CHR(99)||CHR(99)||CHR(99),15)
  • posted 19 days ago by xsjybldb
    1'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'
  • posted 19 days ago by xsjybldb
    1'"
  • posted 19 days ago by xsjybldb
    @@JnPxd
  • posted 12 days ago by 67478559714940000843b0a0
    1
  • posted 12 days ago by 67478559714940000843b0a0
    1
  • posted 12 days ago by 67478559714940000843b0a0
    -1 OR 2+482-482-1=0+0+0+1 --
  • posted 12 days ago by 67478559714940000843b0a0
    -1 OR 2+46-46-1=0+0+0+1
  • posted 12 days ago by 67478559714940000843b0a0
    -1' OR 2+718-718-1=0+0+0+1 --
  • posted 12 days ago by 67478559714940000843b0a0
    -1' OR 2+302-302-1=0+0+0+1 or 'hLC1X0AN'='
  • posted 12 days ago by 67478559714940000843b0a0
    -1" OR 2+875-875-1=0+0+0+1 --
  • posted 12 days ago by 67478559714940000843b0a0
    1*if(now()=sysdate(),sleep(15),0)
  • posted 12 days ago by 67478559714940000843b0a0
    10'XOR(1*if(now()=sysdate(),sleep(15),0))XOR'Z
  • posted 12 days ago by 67478559714940000843b0a0
    10"XOR(1*if(now()=sysdate(),sleep(15),0))XOR"Z
  • posted 12 days ago by 67478559714940000843b0a0
    (select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/
  • posted 12 days ago by 67478559714940000843b0a0
    1-1; waitfor delay '0:0:15' --
  • posted 12 days ago by 67478559714940000843b0a0
    1-1); waitfor delay '0:0:15' --
  • posted 12 days ago by 67478559714940000843b0a0
    1-1 waitfor delay '0:0:15' --
  • posted 12 days ago by 67478559714940000843b0a0
    1eHJPjFNe'; waitfor delay '0:0:15' --
  • posted 12 days ago by 67478559714940000843b0a0
    1-1 OR 202=(SELECT 202 FROM PG_SLEEP(15))--
  • posted 12 days ago by 67478559714940000843b0a0
    1-1) OR 96=(SELECT 96 FROM PG_SLEEP(15))--
  • posted 12 days ago by 67478559714940000843b0a0
    1-1)) OR 683=(SELECT 683 FROM PG_SLEEP(15))--
  • posted 12 days ago by 67478559714940000843b0a0
    1WqTpbcT0' OR 157=(SELECT 157 FROM PG_SLEEP(15))--
  • posted 12 days ago by 67478559714940000843b0a0
    1QOE8LUvh') OR 233=(SELECT 233 FROM PG_SLEEP(15))--
  • posted 12 days ago by 67478559714940000843b0a0
    1gRsXSsZ1')) OR 164=(SELECT 164 FROM PG_SLEEP(15))--
  • posted 12 days ago by 67478559714940000843b0a0
    1*DBMS_PIPE.RECEIVE_MESSAGE(CHR(99)||CHR(99)||CHR(99),15)
  • posted 12 days ago by 67478559714940000843b0a0
    1'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'
  • posted 12 days ago by 67478559714940000843b0a0
    '"
  • posted 12 days ago by 67478559714940000843b0a0
    ����%2527%2522\'\"
  • posted 12 days ago by 67478559714940000843b0a0
    @@gTZNV

sign in to comment