Skip to content

Commit f63209a

Browse files
committed
Add ES6 code samples
1 parent 5677694 commit f63209a

11 files changed

+323
-40
lines changed

README.md

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -462,34 +462,34 @@ Each proposal for an ECMAScript feature goes through the following maturity stag
462462
You can make the object iterable by defining a `Symbol.iterator` property on it.
463463

464464
```js
465-
const collection = {
466-
one: 1,
467-
two: 2,
468-
three: 3,
469-
[Symbol.iterator]() {
470-
const values = Object.keys(this);
471-
let i = 0;
465+
const collection = {
466+
one: 1,
467+
two: 2,
468+
three: 3,
469+
[Symbol.iterator]() {
470+
const values = Object.keys(this);
471+
let i = 0;
472+
return {
473+
next: () => {
472474
return {
473-
next: () => {
474-
return {
475-
value: this[values[i++]],
476-
done: i > values.length
477-
}
478-
}
479-
};
475+
value: this[values[i++]],
476+
done: i > values.length
477+
}
480478
}
481479
};
480+
}
481+
};
482482

483-
const iterator = collection[Symbol.iterator]();
483+
const iterator = collection[Symbol.iterator]();
484484

485-
console.log(iterator.next()); // → {value: 1, done: false}
486-
console.log(iterator.next()); // → {value: 2, done: false}
487-
console.log(iterator.next()); // → {value: 3, done: false}
488-
console.log(iterator.next()); // → {value: undefined, done: true}
485+
console.log(iterator.next()); // → {value: 1, done: false}
486+
console.log(iterator.next()); // → {value: 2, done: false}
487+
console.log(iterator.next()); // → {value: 3, done: false}
488+
console.log(iterator.next()); // → {value: undefined, done: true}
489489

490-
for (const value of collection) {
491-
console.log(value);
492-
}
490+
for (const value of collection) {
491+
console.log(value);
492+
}
493493
```
494494

495495
The for...of statement creates a loop iterating over user defined collection object. But this loop can be used for built-in objects too.
@@ -733,7 +733,7 @@ Each proposal for an ECMAScript feature goes through the following maturity stag
733733
console.log(String.fromCodePoint(134071)); // "𠮷"
734734
```
735735

736-
19. ### Symbols
736+
18. ### Symbols
737737

738738
Symbol is a new peculiar primitive data type of JavaScript, along with other primitive types such as string, number, boolean, null and undefined. The new symbol is created just by calling the Symbol function. i.e, Every time you call the Symbol function, you’ll get a new and completely unique value. You can also pass a parameter to Symbol(), which is useful for debugging purpose only.
739739

@@ -775,7 +775,7 @@ Each proposal for an ECMAScript feature goes through the following maturity stag
775775
console.log(Symbol.for('foo') === Symbol.for('foo')); // true
776776
```
777777

778-
18. ### Proxies
778+
19. ### Proxies
779779
The Proxy object is used to create a proxy for another object, which can intercept and redefine fundamental operations for that object such as property lookup, assignment, enumeration, function invocation etc. These are used in many libraries and some browser frameworks.
780780

781781
The proxy object is created with two parameters with below syntax,
@@ -854,28 +854,28 @@ Each proposal for an ECMAScript feature goes through the following maturity stag
854854
The promise chaining structure would be as below,
855855
856856
```js
857-
const promise = new Promise(function(resolve, reject) {
858-
setTimeout(() => resolve(1), 1000);
859-
});
857+
const promise = new Promise(function(resolve, reject) {
858+
setTimeout(() => resolve(1), 1000);
859+
});
860860
861-
promise.then(function(result) {
861+
promise.then(function(result) {
862862
863-
console.log(result); // 1
864-
return result * 2;
863+
console.log(result); // 1
864+
return result * 2;
865865
866-
}).then(function(result) {
866+
}).then(function(result) {
867867
868-
console.log(result); // 2
869-
return result * 3;
868+
console.log(result); // 2
869+
return result * 3;
870870
871-
}).then(function(result) {
871+
}).then(function(result) {
872872
873-
console.log(result); // 6
874-
return result * 4;
873+
console.log(result); // 6
874+
return result * 4;
875875
876-
}).catch(function(error){
877-
console.log(error);
878-
});
876+
}).catch(function(error){
877+
console.log(error);
878+
});
879879
```
880880
881881
21. ### Reflect
@@ -1002,7 +1002,7 @@ Each proposal for an ECMAScript feature goes through the following maturity stag
10021002
10031003
6. **:**
10041004
1005-
13. ### Binary and Octal
1005+
22. ### Binary and Octal
10061006
ES5 provided numeric literals in octal (prefix 0), decimal (no prefix), and hexadecimal ( 0x) representation. ES6 added support for binary literals and improvements on octal literals.
10071007
10081008
**1. Binary literals:**

es2015/10.iterators-forof.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const collection = {
2+
one: 1,
3+
two: 2,
4+
three: 3,
5+
[Symbol.iterator]() {
6+
const values = Object.keys(this);
7+
let i = 0;
8+
return {
9+
next: () => {
10+
return {
11+
value: this[values[i++]],
12+
done: i > values.length
13+
}
14+
}
15+
};
16+
}
17+
};
18+
19+
const iterator = collection[Symbol.iterator]();
20+
21+
console.log(iterator.next()); // → {value: 1, done: false}
22+
console.log(iterator.next()); // → {value: 2, done: false}
23+
console.log(iterator.next()); // → {value: 3, done: false}
24+
console.log(iterator.next()); // → {value: undefined, done: true}
25+
26+
for (const value of collection) {
27+
console.log(value);
28+
}

es2015/11.generators.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function* myGenerator(i) {
2+
yield i + 10;
3+
yield i + 20;
4+
return i + 30;
5+
}
6+
7+
const myGenObj = myGenerator(10);
8+
9+
console.log(myGenObj.next().value); // 20
10+
console.log(myGenObj.next().value); // 30
11+
console.log(myGenObj.next().value); // 40

es2015/12.modules.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//Export Statement
2+
// Named exports
3+
4+
// module "my-module.js"
5+
6+
const PI = Math.PI;
7+
8+
function add(...args) {
9+
return args.reduce((num, tot) => tot + num);
10+
}
11+
12+
function multiply(...args) {
13+
return args.reduce((num, tot) => tot * num);
14+
}
15+
16+
// private function
17+
function print(msg) {
18+
console.log(msg);
19+
}
20+
21+
export { PI, add, multiply };
22+
23+
// Default exports
24+
// module "my-module.js"
25+
26+
export default function add(...args) {
27+
return args.reduce((num, tot) => tot + num);
28+
}
29+
30+
//Import Statement
31+
32+
// 1. Import an entire module's contents
33+
import * as name from "my-module";
34+
35+
//2.Import a single export from a module
36+
import { export1 } from "my-module";
37+
38+
//3.Import multiple exports from a module
39+
import { export1 , export2 } from "my-module";
40+
41+
//4.Import default export from a module
42+
import defaultExport from "my-module";
43+
44+
//5.Import an export with an alias
45+
import { export1 as alias1 } from "my-module";
46+

es2015/17.unicode.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
let str = '𠮷';
2+
3+
// new string form
4+
console.log('\u{20BB7}'); // "𠮷"
5+
6+
// new RegExp u mode
7+
console.log(new RegExp('\u{20BB7}', 'u'));
8+
console.log(/^.$/u.test(str)); // true
9+
10+
//API methods
11+
console.log(str.codePointAt(0)); // 134071
12+
console.log(str.codePointAt(1)); // 57271
13+
14+
console.log(String.fromCodePoint(134071)); // "𠮷"

es2015/18.symbols.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//1. Object properties
2+
let id = Symbol("id");
3+
let user = {
4+
name: "John",
5+
age: 40,
6+
[id]: 111
7+
};
8+
9+
for (let key in user) {
10+
console.log(key); // name, age without symbols
11+
}
12+
13+
console.log(JSON.stringify(user)); // {"name":"John", "age": 40}
14+
console.log(Object.keys(user)); // ["name", "age"]
15+
16+
console.log( "User Id: " + user[id] ); // Direct access by the symbol works
17+
18+
//2. Unique constants
19+
const logLevels = {
20+
DEBUG: Symbol('debug'),
21+
INFO: Symbol('info'),
22+
WARN: Symbol('warn'),
23+
ERROR: Symbol('error'),
24+
};
25+
console.log(logLevels.DEBUG, 'debug message');
26+
console.log(logLevels.INFO, 'info message');
27+
28+
//3. Equality Checks
29+
30+
console.log(Symbol('foo') === Symbol('foo')); // false
31+
console.log(Symbol.for('foo') === Symbol.for('foo')); // true

es2015/19.proxies.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const target = {
2+
name: "John",
3+
age: 3
4+
};
5+
6+
const handler = {
7+
get: function(target, prop) {
8+
return prop in target ?
9+
target[prop] :
10+
`${prop} does not exist';
11+
}
12+
};
13+
14+
const user = new Proxy(target, handler);
15+
console.log(user.name); // John
16+
console.log(user.age); // John
17+
console.log(user.gender); // gender does not exist
18+
19+
// validations
20+
21+
let ageValidator = {
22+
set: function(obj, prop, value) {
23+
if (prop === 'age') {
24+
if (!Number.isInteger(value)) {
25+
throw new TypeError('The age is not an integer');
26+
}
27+
if (value > 200) {
28+
throw new RangeError('Invalid age');
29+
}
30+
}
31+
32+
obj[prop] = value; // The default behavior to store the value
33+
34+
return true; // Indicate success
35+
}
36+
};
37+
38+
const person = new Proxy({}, validator);
39+
40+
person.age = 30;
41+
console.log(person.age); // 30
42+
person.age = 'old'; // Throws an exception
43+
person.age = 200; // Throws an exception

es2015/20.promises.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const promise = new Promise(function(resolve, reject) {
2+
setTimeout(() => resolve(1), 1000);
3+
});
4+
5+
promise.then(function(result) {
6+
7+
console.log(result); // 1
8+
return result * 2;
9+
10+
}).then(function(result) {
11+
12+
console.log(result); // 2
13+
return result * 3;
14+
15+
}).then(function(result) {
16+
17+
console.log(result); // 6
18+
return result * 4;
19+
20+
}).catch(function(error){
21+
console.log(error);
22+
});

0 commit comments

Comments
 (0)