Skip to content

Instantly share code, notes, and snippets.

@richard-to
Last active September 1, 2016 00:32
Show Gist options
  • Save richard-to/70049650cf58f1858b8cd38528c07a3c to your computer and use it in GitHub Desktop.
Save richard-to/70049650cf58f1858b8cd38528c07a3c to your computer and use it in GitHub Desktop.
ES6 Features - based on examples from http://es6-features.org/
// Constants
const test_const = 1;
// Scoped variables - use `let` for local scope
let callbacks = [];
for (let i = 0; i < 3; ++i) {
callbacks[i] = function() { return i; };
}
// Arrow functions
let plus_one = [1, 2, 3, 5].map(v => v + 1);
let fives = [];
[1, 4, 5, 6].forEach(v => {
if (v % 5 === 0) {
fives.push(v);
}
});
// Arrow functions + Lexical "this" - A bit confusing actually...Ok, so arrow functions do not use their own "this"
this.fives = [];
this.nums = [1, 4, 5, 6];
this.nums.forEach((v) => {
if (v % 5 === 0) {
this.fives.push(v);
}
});
// Function defaults
function f_with_defaults(x, y=7, z=42) {
return x + y + z;
}
f_with_defaults(1) === 50;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment