June 14, 201511 yr For those that are not aware ES6 is the new edition of Ecma Script AKA JavaScript that is due to become a standard this month. It has some great features such as: Let statement that respects block scope, unlike Var that uses function scope, so now for example if you were declare a let variable inside a loop it will be scoped to that loop only. Arrow functions where let foo = (x,y) => x*y; is equivalent to let foo = function foo(x, y) { return x * y; }; At first this just seems like some syntactic sugar, but I actually think it's more descriptive. We could write a reduce function to give us the total of an array of numbers like this: let nums = [1,2,3,4,5]; let totalAll = (arr) => arr.reduce((prevVal, currVal) => prevVal + currVal); console.log(totalAll(nums)); // 15 and that would be equivalent to writing var nums = [1, 2, 3, 4, 5]; var totalAll = function totalAll(arr) { return arr.reduce(function (prevVal, currVal) { return prevVal + currVal; }); }; console.log(totalAll(nums)); // 15 this will also have the benefit of reducing filesize slightly without all those 'function' statements.EDIT: I should also add that arrow functions lexically bind the 'this' value to the declared context of the function, rather than it being bound to the call site like a typical function call, for this reason we should not replace all functions with arrow functions otherwise things will break, but it will reduce the need for us to alias the 'this' keyword. This is where we should use arrow functions, and of course for functions that don't use 'this' can be replaced with arrow functions. Rest parameters - Similar to the arguments parameter that every function gets, but this is a real array so there's no need to run Array.prototype.someArrayMethod.call(arguments) to do a forEach() or a map(). Rest parameters for those not so familiar with the arguments object is a collection of all the parameters passed. Rest parameters differ in that its a true array consisting of all the remaining parameters passed in.Example: var totalAll = function(...params) { return params.reduce(function(prevVal, currVal) { return prevVal + currVal; }); }; console.log(totalAll(1,2,3,4)); // 10 Example using Let and arrow functions and we can get it all on a single line: let newTotalAll = (...params) => params.reduce((prevVal, currVal) => prevVal + currVal); console.log(newTotalAll(1,2,3,4)); // 10 If we were to do exactly the same using ES5 we'd have to do: var newTotalAll = function() { return Array.prototype.reduce.call(arguments, function(prevVal, currVal) { return prevVal + currVal; }); }; console.log(newTotalAll(1,2,3,4)); // 10 Which I think is much harder to read. There's a whole bunch of other good stuff, this is just some stuff I've been playing around in so far. You can use ES6 in your projects right away using a transpiler such as https://babeljs.io which is available as a Grunt.js plugin, it will transpile the ES6 code into ES5 as part of your build process. Babel have a live editor which is great for learning, it will show the ES5 equivalent to the code you are writing https://babeljs.io/repl/ Seen as this is going to be a standard very soon it is well worth learning. There is a fantastic course over on Frontend Masters that covers all the fundamentals for ES6: https://frontendmasters.com/courses/jsnext-es6/ Edited June 15, 201511 yr by rbrtsmith
June 15, 201511 yr Author Here's a really good talk covering a lot of ES6 (Good parts) by Douglas Crockford. There's some bad parts too according to Crockford like classes, when I look at his explanation I agree 100%. Would be interested to see the views of others on this.. Funny quote from Douglas: "Those people (Who use classes) will go to their graves never knowing how miserable they are" Edited June 15, 201511 yr by rbrtsmith
Create an account or sign in to comment