ES6: formatting

This commit is contained in:
Rico Sta. Cruz 2015-02-24 16:13:25 +08:00
parent 69542c60a1
commit 1b5febfd69
1 changed files with 43 additions and 34 deletions

75
es6.md
View File

@ -7,11 +7,9 @@ layout: default
New features you can use on io.js. New features you can use on io.js.
```js ### Promises
/*
* Promises
*/
```js
new Promise(fn) new Promise(fn)
.then(fn) .then(fn)
.catch(fn) .catch(fn)
@ -20,11 +18,11 @@ Promise.all(/*...*/)
Promise.race(/*...*/) Promise.race(/*...*/)
Promise.reject(/*...*/) Promise.reject(/*...*/)
Promise.resolve(/*...*/) Promise.resolve(/*...*/)
```
/* ### Block scoping
* Block scoping
*/
```js
// Block scoping (let) // Block scoping (let)
function fn () { function fn () {
let x = 0; let x = 0;
@ -35,20 +33,28 @@ function fn () {
// Constants // Constants
const a = 1; const a = 1;
```
/* ### Backtick strings
* Syntax updates, etc
*/
// Short object syntax ```js
module.exports = { // interpolation
sayHello,
sayGoodbye
};
// Template strings
var message = `Hello ${name}`; var message = `Hello ${name}`;
// Multiline
var str = `
hello
world
`;
```
### Syntax improvements
```js
// Short object syntax
// aka: `exports = { hello:hello, bye:bye }`
module.exports = { hello, bye };
// New string methods // New string methods
"hello".repeat(3) "hello".repeat(3)
"hello".contains("ll") "hello".contains("ll")
@ -63,11 +69,9 @@ var oct = 0755;
Available via [Babel] Available via [Babel]
```js ### Module imports
/*
* Imports
*/
```js
// Import; aka: require('...') // Import; aka: require('...')
import 'helpers'; import 'helpers';
@ -76,11 +80,11 @@ import Express from 'express';
// Import; aka: indent = require('...').indent // Import; aka: indent = require('...').indent
import { indent } from 'helpers'; import { indent } from 'helpers';
```
/* ### Module exports
* Exports
*/
```js
// Export; aka: module.exports = ... // Export; aka: module.exports = ...
export default function () { export default function () {
// ... // ...
@ -92,11 +96,11 @@ export function mymethod () {
// Export a value; aka: exports.pi = ... // Export a value; aka: exports.pi = ...
export var pi = 3.14159; export var pi = 3.14159;
```
/* ### Destructuring
* Destructuring
*/
```js
// Destructuring assignment // Destructuring assignment
var [first, last] = ["Nikola", "Tesla"]; var [first, last] = ["Nikola", "Tesla"];
let {title, author} = { title: "The Silkworm", author: "R. Galbraith" }; let {title, author} = { title: "The Silkworm", author: "R. Galbraith" };
@ -112,11 +116,11 @@ function greet({ name, greeting }) {
} }
greet({ name: "Larry", greeting: "Ahoy" }); greet({ name: "Larry", greeting: "Ahoy" });
```
/* ### Function arguments
* Spread
*/
```js
// Default arguments // Default arguments
function greet(name="Jerry") { function greet(name="Jerry") {
return `Hello ${name}`; return `Hello ${name}`;
@ -130,16 +134,21 @@ function fn(x, ...y) {
// Rest // Rest
fn(...[1,2,3]) // same as fn(1,2,3) fn(...[1,2,3]) // same as fn(1,2,3)
```
### Fat arrows
```js
// Fat arrows // Fat arrows
setTimeout(() => { setTimeout(() => {
console.log('hi'); console.log('hi');
}); });
// Short syntax (no `return` without `{}`)
numbers.map(n => n * 2)
``` ```
## Classes ### Classes
Available in [Babel]
```js ```js
class Circle extends Shape { class Circle extends Shape {