ES6

References:

Class

class Point {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }

  toString() {
    return '(' + this.x + ', ' + this.y + ')';
  }
}

Style Guide

Guide: https://github.com/airbnb/javascript

  • DON'T use 'single var pattern'

  • new line

  • Don't use parentheses for unary operator such as delete, void, typeof

  • Use single quote '' for strings

  • Use function declarations instead of function expressions.

How to run ES6 in Node

index.js

es6code.js

Iteration

  • for...in: Iterate over property name, in arbitrary order (because property doesn't have order)

  • ES6: Iterate over values: for .. of

Module

http://exploringjs.com/es6/ch_modules.html

ES5

CommonJS Modules: The dominant implementation of this standard is in Node.js (Node.js modules have a few features that go beyond CommonJS). Characteristics:

  • Compact syntax

  • Designed for synchronous loading

  • Main use: server

Asynchronous Module Definition (AMD): The most popular implementation of this standard is RequireJS. Characteristics:

  • Slightly more complicated syntax, enabling AMD to work without eval() (or a compilation step).

  • Designed for asynchronous loading

  • Main use: browsers

ES6

Declarative syntax (for importing and exporting)

Default exports If you want to export only a single function, you have to use export default then you can do import a from 'MyClass'

Programmatic loader API: to configure how modules are loaded and to conditionally load modules

Block / Scope

  • ES5只有全局作用域和函数作用域,没有块级作用域

  • ES6: let实际上为JavaScript新增了块级作用域; 函数本身的作用域,在其所在的块级作用域之内。

Spread/Rest

Use for collect the rest:

Use for spread object or array into parameters like:

  • shape: {a, b} -> a, b

  • shape: [a, b] -> a, b

Last updated