David Beveridge

Software Artisan, JavaScript Afficionado

ECMAScript 6 - Default, Rest, and Spread Arguments

Ever wanted a default value for an arugment? Or the ability to work with
variable numbers of arguments more easily? Ever written code like this?

1
2
3
4
function(foo, bar) {
  bar = bar || 'default value';
  var theRest = [].slice.call(arguments, 2);
}

Perhaps you're familiar with CoffeeScript's splat arguments
(...); or, if you've ever used Ruby, you'll be familiar with
the Ruby splat operator.

Maybe you've needed to use an array as an argument list? Ever done this?

1
2
var args = [1,2,3];
doSomethingWithArgs.apply(null, args);

The days of using [].slice.call and fn.apply(null, args) are over!
Finally, JavaScript is introducing a splat-like operator!
Say hello to
default, rest, and spread arguments!

ECMAScript 6: Arrow Functions

One of my favorite proposals for ECMAScript 6 is the proposal to add a
convenient labmda syntax: Arrow Functions.

Personally, I get sick of typing function (){} all the time (ok, I don't
usually type it, my editor takes care of it).

Say I want to do some functional stuff (silly example):

1
2
3
4
5
var numbers = [0,1,2,3,4,5,6,7,8,9];
// map a list of even/odd pairs, multiplied
numbers
  .filter( (x) => { return x % 2 === 0;} )
  .map( (x, i) => { return x * numbers[i+1]; } );

Yay! Much more convenient. But, arrow functions are not exactly like
functions. How, you ask?