babel/FEATURES.md
2014-10-13 03:26:13 +11:00

1.9 KiB

Features

Array comprehension

[for (i of [1, 2, 3]) i * i]; // [1, 4, 9]

Arrow functions

arr.map(x => x * x);

Let scoping

for (let i in arr) {
  let v = arr[i];
}

Classes

class Foo extends Bar {
  constructor() { }

  foo() { }

  get bar() { }

  set bar() { }
}

Computed property names

var foo = "foo";
var bar = "bar";
var obj = {
  ["foo" + bar]: "heh",
  ["bar" + foo]: "noo",
  foo: "foo",
  bar: "bar"
};

Constants

const MULTIPLIER = 5;
console.log(2 * MULTIPLIER);

MULTIPLIER = 6; // error
var MULTIPLIER; // error

Default parameters

function foo(bar = "foo") {
  return bar + "bar";
}

Destructuring

var [a, [b], c, d] = ["hello", [", ", "junk"], ["world"]];
console.log(a + b + c); // hello, world

For-of

for (var i of [1, 2, 3]) {
  console.log(i * i);
}

Generators

Modules

Numeric literals

0b111110111 === 503; // true
0o767 === 503; // true

Property method assignment

var obj = {
  foo() {
    return "foobar";
  },

  get bar() {
    return this._bar;
  },

  set bar(val) {
    this._bar = val;
  }
};

Property name shorthand

function f(x, y) {
  return { x, y };
}

Rest parameters

function printList(name, ...items) {
  console.log("list %s has the following items", name);
  items.forEach(function (item) {
    console.log(item);
  });
}

Spread

function add(x, y) {
  return x + y;
}

var numbers = [5, 10];
add(...numbers); // 15

Template literals

var x = 5;
var y = 10;
console.log(`${x} + ${y} = ${x + y}`); // "5 + 10 = 15"

Unicode regex

var string = 'foo💩bar';
var match = string.match(/foo(.)bar/u);
console.log(match[1]);