Update es6
This commit is contained in:
parent
7213d46806
commit
f7d88ba0de
142
es6.md
142
es6.md
|
@ -3,57 +3,147 @@ title: ES6
|
||||||
layout: default
|
layout: default
|
||||||
---
|
---
|
||||||
|
|
||||||
## Stable
|
## Stable (io.js)
|
||||||
|
|
||||||
New features you can use on io.js.
|
New features you can use on io.js.
|
||||||
|
|
||||||
### Promises
|
```js
|
||||||
|
/*
|
||||||
|
* Promises
|
||||||
|
*/
|
||||||
|
|
||||||
new Promise(fn)
|
new Promise(fn)
|
||||||
.then(fn)
|
.then(fn)
|
||||||
.catch(fn)
|
.catch(fn)
|
||||||
|
|
||||||
Promise.all(...)
|
Promise.all(/*...*/)
|
||||||
Promise.race(...)
|
Promise.race(/*...*/)
|
||||||
Promise.reject(...)
|
Promise.reject(/*...*/)
|
||||||
Promise.resolve(...)
|
Promise.resolve(/*...*/)
|
||||||
|
|
||||||
### [Template strings](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/template_strings)
|
/*
|
||||||
|
* Destructuring
|
||||||
|
*/
|
||||||
|
|
||||||
var message = `Hello there ${name}`;
|
// Destructuring assignment
|
||||||
|
var [first, last] = ["Nikola", "Tesla"];
|
||||||
|
let {title, author} = { title: "The Silkworm", author: "R. Galbraith" };
|
||||||
|
|
||||||
### [Block scoping](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let)
|
|
||||||
|
|
||||||
if (true) {
|
// Available in loops too
|
||||||
let x = 1;
|
for (let {title, artist} in songs) {
|
||||||
|
// ...
|
||||||
}
|
}
|
||||||
|
|
||||||
### [Constants](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const)
|
/*
|
||||||
|
* Block scoping
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Block scoping (let)
|
||||||
|
function fn () {
|
||||||
|
let x = 0;
|
||||||
|
if (true) {
|
||||||
|
let x = 1; // only inside this `if`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Constants
|
||||||
const a = 1;
|
const a = 1;
|
||||||
|
|
||||||
### Generators
|
/*
|
||||||
|
* Syntax updates, etc
|
||||||
|
*/
|
||||||
|
|
||||||
function*() {...}
|
// Short object syntax
|
||||||
|
module.exports = {
|
||||||
|
sayHello,
|
||||||
|
sayGoodbye
|
||||||
|
};
|
||||||
|
|
||||||
### Binary/octal
|
// Template strings
|
||||||
|
var message = `Hello ${name}`;
|
||||||
var bin = 0b1010010;
|
|
||||||
var oct = 0755;
|
|
||||||
|
|
||||||
### String methods
|
|
||||||
|
|
||||||
|
// New string methods
|
||||||
"hello".repeat(3)
|
"hello".repeat(3)
|
||||||
"hello".contains("ll")
|
"hello".contains("ll")
|
||||||
"\u1E9B\u0323".normalize("NFC")
|
"\u1E9B\u0323".normalize("NFC")
|
||||||
|
|
||||||
### [Symbols](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol)
|
// Binary/octal literals
|
||||||
|
var bin = 0b1010010;
|
||||||
|
var oct = 0755;
|
||||||
|
```
|
||||||
|
|
||||||
var sym = Symbol("foo")
|
## Stable (6to5)
|
||||||
typeof sym === 'symbol'
|
|
||||||
|
|
||||||
### [Collections](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map)
|
Available via 6to5
|
||||||
|
|
||||||
Map, WeakMap
|
```js
|
||||||
Set, WeakSet
|
/*
|
||||||
|
* Imports
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Import; aka: require('...')
|
||||||
|
import 'helpers';
|
||||||
|
|
||||||
|
// Import; aka: Express = require('...')
|
||||||
|
import Express from 'express';
|
||||||
|
|
||||||
|
// Import; aka: indent = require('...').indent
|
||||||
|
import { indent } from 'helpers';
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Exports
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Export; aka: module.exports = ...
|
||||||
|
export default function () {
|
||||||
|
// ...
|
||||||
|
};
|
||||||
|
|
||||||
|
// Export a method; aka: exports.mymethod = ...
|
||||||
|
export function mymethod () {
|
||||||
|
};
|
||||||
|
|
||||||
|
// Export a value; aka: exports.pi = ...
|
||||||
|
export var pi = 3.14159;
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Spread
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Default arguments
|
||||||
|
function greet(name="Jerry") {
|
||||||
|
return `Hello ${name}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Spread arguments
|
||||||
|
function fn(x, ...y) {
|
||||||
|
// y is an Array
|
||||||
|
return x * y.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rest
|
||||||
|
fn(...[1,2,3]) // same as fn(1,2,3)
|
||||||
|
|
||||||
|
// Fat arrows
|
||||||
|
setTimeout(() => {
|
||||||
|
console.log('hi');
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
## Experimental
|
||||||
|
|
||||||
|
Available via 6to5's experimental mode
|
||||||
|
|
||||||
|
```js
|
||||||
|
/*
|
||||||
|
* Comprehensions
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Basic comprehension
|
||||||
|
var names = [for (c of customers) c.name];
|
||||||
|
|
||||||
|
// Comprehension with IDs
|
||||||
|
var names = [for (c of customers) if (c.admin) c.name];
|
||||||
|
```
|
||||||
|
|
|
@ -57,6 +57,12 @@ article:section
|
||||||
article:tag
|
article:tag
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Favicon
|
||||||
|
|
||||||
|
```html
|
||||||
|
<link rel="icon" type="image/png" href="/assets/favicon.png">
|
||||||
|
```
|
||||||
|
|
||||||
### Reference
|
### Reference
|
||||||
|
|
||||||
* https://dev.twitter.com/docs/cards
|
* https://dev.twitter.com/docs/cards
|
||||||
|
|
Loading…
Reference in New Issue