Skip to content

Instantly share code, notes, and snippets.

@cmalven
Last active November 9, 2015 18:06
Show Gist options
  • Save cmalven/1b816b046f52a14dde50 to your computer and use it in GitHub Desktop.
Save cmalven/1b816b046f52a14dde50 to your computer and use it in GitHub Desktop.
//
// ECMAScript 6 Cheat Sheet
//
//////////////////////////////////////////////////////////////////////
//---------------------------------------------------------------
// Arrows (and this)
//---------------------------------------------------------------
// Expression bodies
var odds = evens.map(v => v + 1);
var nums = evens.map((v, i) => v + i);
// Statement bodies
nums.forEach(v => {
if (v % 5 === 0)
fives.push(v);
});
//---------------------------------------------------------------
// Modules
//---------------------------------------------------------------
// lib/math.js
export function sum(x, y) {
return x + y;
}
export var pi = 3.141593;
// app.js
import * as math from "lib/math";
alert("2π = " + math.sum(math.pi, math.pi));
// otherApp.js
import {sum, pi} from "lib/math";
alert("2π = " + sum(pi, pi));
// lib/mathplusplus.js
export * from "lib/math";
export var e = 2.71828182846;
export default function(x) {
return Math.exp(x);
}
// app.js
import exp, {pi, e} from "lib/mathplusplus";
alert("2π = " + exp(pi, e));
//---------------------------------------------------------------
// Classes
//---------------------------------------------------------------
export default class AnimPanel {
constructor(options = {}) {
// options:
// \$el: The jQuery selector that defines each description
// (required)
//
// Internal Variables
//
this.options = $.extend(this.options, {
name: 'Default Name'
});
this.foo = 'bar';
_init();
}
//
// Private Methods
//
//////////////////////////////////////////////////////////////////////
_init() {
_addEventListeners();
}
_addEventListeners() {
}
//
// Public Methods
//
//////////////////////////////////////////////////////////////////////
someMethod() {
}
static staticMethod() {
}
}
//---------------------------------------------------------------
// Template Strings
//---------------------------------------------------------------
// Basic literal string creation
let text = `This is a pretty little template string.`
// Multiline strings
`In ES5 this is
not legal.`
// Interpolate variable bindings
var name = "Bob", time = "today";
`Hello ${name}, how are you ${time}?`
// Unescaped template strings
String.raw`In ES5 "\n" is a line-feed.`
//---------------------------------------------------------------
// Default + Rest + Spread
//---------------------------------------------------------------
function f(x, y=12) {
// y is 12 if not passed (or passed as undefined)
return x + y;
}
f(3) == 15
function f(x, ...y) {
// y is an Array
return x * y.length;
}
f(3, "hello", true) == 6
function f(x, y, z) {
return x + y + z;
}
// Pass each elem of array as argument
f(...[1,2,3]) == 6
//---------------------------------------------------------------
// Let + Const
//---------------------------------------------------------------
function f() {
{
let x;
{
// okay, block scoped name
const x = "sneaky";
// error, const
x = "foo";
}
// okay, declared with `let`
x = "bar";
// error, already declared in block
let x = "inner";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment