As of a65e you can now use more ES6 features rather than just modules, via the excellent 6to5 transpiler.
I've whitelisted the following features for Discourse because I feel they are a huge improvement over regular JS and the transpiled output is very similar to what you'd write anyway. These are all features that are approved for ES6 and will be natively supported in browsers (some features already are).
Better Object Literals
You don't need the function
word anymore!
ES5: { render: function() { ... }
ES6: { render() { ... } }
If you are just setting an object key to be a local variable, you don't need to repeat yourself:
ES5: { topic: topic, post: post }
ES6: { topic, post }
Arrow functions
If you're doing a simple anonymous function, Javascript can be quite verbose:
ES5: [1,2,3].map(function(x) { return x * 2; })
ES6: [1,2,3].map(x => x * 2)
Note that using the arrow automatically binds to the outer block too, so you don't have to use self = this
or bind
.
const
If you know a variable never changes, prenpend it with const
ES5: var x = 123;
ES6: const x = 123;
If you do this, JSHint will enforce that the variable never changes (it's transpiled to a var for browsers).
let
Using let
you can properly scope variables to blocks. Otherwise they are scoped to the entire function.
function something() {
if (true) {
var x = 123; // this is scoped to the function!
let y = 123; // this is scoped to the if block!
}
}