Skip to content

Latest commit

 

History

History
271 lines (214 loc) · 4.59 KB

es6.md

File metadata and controls

271 lines (214 loc) · 4.59 KB
title category
ES2015
JavaScript

Stable in io.js

New features you can use on io.js. {:.brief-intro.center.top-space-0}

For asynchronous programming.

new Promise(fn)
  .then(fn)
  .catch(fn)
Promise.all(/*...*/)
Promise.race(/*...*/)
Promise.reject(/*...*/)
Promise.resolve(/*...*/)

let is the new var.

// Block scoping (let)
function fn () {
  let x = 0;
  if (true) {
    let x = 1; // only inside this `if`
  }
}
// Constants
const a = 1;

Templates and multiline strings.

// Interpolation
var message = `Hello ${name}`;
// Multiline
var str = `
hello
world
`;

Other improvements

New string methods, binary and octal numbers.

// New string methods
"hello".repeat(3)
"hello".contains("ll")
"\u1E9B\u0323".normalize("NFC")
// Binary/octal literals
var bin = 0b1010010;
var oct = 0755;

Adds support for getters, setters, methods, shorthand.

// Short object syntax
// aka: `exports = { hello:hello, bye:bye }`
module.exports = { hello, bye };
App = {
  // shorthand for `handler: handler`
  handler,

  // methods
  start() {
    this.go();
  },

  // getter/setter
  get closed() {
    return this.status === 'closed';
  },

  // custom prototypes
  __proto__: protoObj,

  // computed property names
  [ "prop_"+n ]: 42
};

It's complicated.

function* idMaker() {
  var id = 0;
  while (true) { yield id++; }
}

var gen = idMaker();
gen.next().value  // 0
gen.next().value  // 1
gen.next().value  // 2

Syntactic sugar for prototypes.

class Circle extends Shape {
  // ctor
  constructor(radius) {
    this.radius = radius;
  }

  // methods
  getArea() {
    return Math.PI * 2 * this.radius;
  }

  // calling super methods
  expand(n) {
    return super.expand(n) * Math.PI;
  }

  // static methods
  static createFromDiameter(diameter) {
    return new Circle(diameter / 2);
  }
}

Stable in Babel

Available via the Babel transpiler. {:.brief-intro.center.top-space-0}

import is the new require().

// aka: require('...')
import 'helpers';

// aka: Express = require('...')
import Express from 'express';

// aka: indent = require('...').indent
import { indent } from 'helpers';

// aka: indent = require('...').indentSpaces
import { indentSpaces as indent } from 'helpers';

export is the new module.exports =.

// aka: module.exports = ...
export default function () {
  // ...
};

// aka: exports.mymethod = ...
export function mymethod () {
};

// aka: exports.pi = ...
export var pi = 3.14159;

Supports for matching arrays and objects.

// Destructuring assignment
var [first, last] = ["Nikola", "Tesla"];
let {title, author} = { title: "The Silkworm", author: "R. Galbraith" };
// Available in loops too
for (let {title, artist} in songs) {
  // ...
}
// Functions
function greet({ name, greeting }) {
  // ...
}

greet({ name: "Larry", greeting: "Ahoy" });

Default, rest, spread. (iojs: --harmony-rest-parameters)

// Default arguments
function greet(name = "Jerry") {
  return `Hello ${name}`;
}
// Rest arguments
function fn(x, ...y) {
  // y is an Array
  return x * y.length;
}
// Spread
fn(...[1,2,3]) // same as fn(1,2,3)

Like functions but with this preserved.

// Fat arrows
setTimeout(() => {
  ...
});
// With arguments
readFile('text.txt', (err, data) => {
  ...
});
// Short syntax (no `return` without `{}`)
numbers.map(n => n * 2)

For iterating through generators and arrays.

for (let i of iterable) {
  // ...
}