Skip to content

Commit e50f63c

Browse files
committed
Initial commit
0 parents  commit e50f63c

File tree

6 files changed

+149
-0
lines changed

6 files changed

+149
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# ES6
2+
3+
> ES6 feature documentation and examples
4+
5+
Run all the examples,
6+
7+
```sh
8+
npm start
9+
```
10+
11+
Run a particular example,
12+
13+
```sh
14+
npm start --es6:example=arrow-functions
15+
```
16+
17+
Watch an example and re-run when it changes,
18+
19+
```sh
20+
npm run watch --es6:example=arrow-functions
21+
```
22+
23+
## Table of contents
24+
25+
- [Arrow functions](#arrow-functions)
26+
27+
## Arrow functions
28+
29+
Arrow functions have a concise function syntax, and their `this` value is lexically bound to their enclosing scope.
30+
31+
```js
32+
var add = (a,b) => a+b;
33+
add(1,1); // 2
34+
```
35+
> When the arrow function body is a single expression it is implicitly returned.
36+
37+
```js
38+
var odd = n => n % 2;
39+
odd(2); // 0
40+
```
41+
> When the arrow function has only one argument the parens can be omitted.
42+
43+
```js
44+
var ran = () => Math.random();
45+
ran() // 0.265058204298839
46+
```
47+
> When the arrow function has no arguments you need the empty `()`.
48+
49+
```js
50+
var transform = s => {
51+
s = s.toUpperCase();
52+
return s + s;
53+
}
54+
transform('a'); // AA
55+
```
56+
> When the arrow function body has multiple statements then `{}` are required.
57+
58+
```js
59+
function Counter () {
60+
this.count = 0;
61+
setInterval(() => this.count++, 1000);
62+
}
63+
```
64+
> The arrow function's `this` value is bound to the enclosing scope, so no more need for `var self = this`.
65+
66+
```js
67+
var lengths = ['one', 'two', 'three'].map(s => s.length);
68+
// [3, 3, 5]
69+
```
70+
> Arrow functions can be used to write iterators and map functions quite concisely.

index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require('babel/register');
2+
3+
var chalk = require('chalk');
4+
var fs = require('fs');
5+
6+
var example = process.env.npm_package_config_example;
7+
8+
if ( example ) {
9+
run(example);
10+
} else {
11+
runAll();
12+
}
13+
14+
function run (example) {
15+
console.log('\n' + chalk.yellow(example) + '\n');
16+
require('./js/' + example);
17+
}
18+
19+
function runAll () {
20+
var examples = fs.readdirSync('./js');
21+
examples.forEach(run);
22+
}

js/arrow-functions.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var assert = require('assert');
2+
3+
// Simple
4+
5+
var add = (a,b) => a+b;
6+
7+
assert.equal(add(1,1),2);
8+
9+
// Single arg
10+
11+
var odd = n => n % 2;
12+
13+
assert.equal(odd(2),false);
14+
assert.equal(odd(3),true);
15+
console.log(Math.random());
16+
// Lexical this
17+
18+
function TimeoutTest () {
19+
this.count = 0;
20+
setTimeout(() => this.count++, 100);
21+
setTimeout(() => assert.equal(this.count,1), 200);
22+
}
23+
24+
var timeoutTest = new TimeoutTest();
25+
26+
// Map
27+
28+
var lengths = ['one', 'two', 'three'].map(s => s.length);
29+
assert.deepEqual(lengths, [3, 3, 5]);

package.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "es6",
3+
"version": "0.0.1",
4+
"description": "ES6 Features",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "node index.js",
8+
"watch": "node watch.js"
9+
},
10+
"dependencies": {
11+
"babel": "^5.0.12",
12+
"chalk": "^1.0.0",
13+
"nodemon": "^1.3.7"
14+
},
15+
"engines": {
16+
"node": "^0.12.0",
17+
"npm": "^2.6.0"
18+
}
19+
}

watch.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var nodemon = require('nodemon');
2+
3+
var example = process.env.npm_package_config_example;
4+
5+
nodemon({
6+
exec: 'npm start --es6:example=' + example,
7+
watch: ['js']
8+
});

0 commit comments

Comments
 (0)