ES6
- var -->
let
&const
- function expressions --> Arrow functions
- constructors --> classes
- Destructuring MDN
for ... of
Loops- Method Definitions (in objects)
- arguments --> Rest Parameters
- Spread Operator MDN
- objects --> maps
- Subclasses of Error
- Template Literals
- ES6 Modules
- Iterables and Iterators
- Generators
for...of vs. for...in
for...of
Iterates over Iterable objects, such as an array.
let nicknames = ['foo', 'bar', 'buzz'];
for (let nickname of nicknames){
console.log(nickname);
}
// result: foo, bar, buzz
for...in
Iterates over the properties of an object.
let nicknames = ['foo', 'bar', 'buzz'];
for (let nickname in nicknames){
console.log(nickname);
}
// result: 0, 1, 2
ES6 Modules
Workflow:
Write ES6 modules --> Transpile (Babel) --> (AMD or CommonJS) --> module loader (RequireJS or SystemJS)
Exporting:
- add
export
in front of the definition - add
export default
in front of the definition to assign a Default Export
or
- add all exports to an
export { }
function at the bottom of the file - add
as
to an export statement to alias an export
Importing:
- import an entire module with
*.
import * as somevar from './path/to/file.js'
import { } from './file.js'
- add
as
to an import statement to create an alias for the import. (helpful for adding context)
Examples:
Named Exports
var theFunq = function(){
// funky function
}
var yoThing = 'Some String';
export { theFunq as daFunk, yoThing }
Default Exports
export default function addResult(newResult){
return newResult + 1;
}
function somePrivateFunction() {
// not part of the API
}
export var theFunq = function(){
// funky function
}
Bulk Import
import * as someVar from './path/to/file.js';
someVar.theFunq();
Named Imports
import { daFunk as funq, yoThing } from './path/to/file.js';
funq(myThang);
Default Imports
import addResult, { theFunq } from './path/to/file.js';
Native Promises
The core idea behind promises is that a promise represents the result of an asynchronous operation.
A promise is in one of three different states:
- pending - The initial state of a promise.
- fulfilled - The state of a promise representing a successful operation.
- rejected - The state of a promise representing a failed operation.
Once a promise is fulfilled or rejected, it is immutable (i.e. it can never change again).
Rest & Spread
The rest operator
let { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 };
x; // 1
y; // 2
z; // { a: 3, b: 4 }
The spread operator
let n = { x, y, ...z };
n; // { x: 1, y: 2, a: 3, b: 4 }