Skip to content
This repository was archived by the owner on Feb 17, 2024. It is now read-only.

Commit 1943286

Browse files
committed
chore: init source
0 parents  commit 1943286

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+2911
-0
lines changed

Diff for: 01-es6/1.variable-declarations.ts

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
/*
3+
* New ES6 variable declarations with `let` and `const`
4+
*
5+
*/
6+
7+
8+
console.log('-let-');
9+
try {
10+
11+
// `let` vs `var`
12+
// While `var` creates a variable scoped within its nearest parent function,
13+
// let scopes the variable to the nearest block
14+
function variableDeclarationsLetVar() {
15+
console.log('before: foo:', foo); // => undefined
16+
17+
var foo = 'IamVar';
18+
if (foo === 'IamVar') {
19+
let foo = 'IamLet';
20+
}
21+
22+
console.log('after: foo:', foo); // => 'IamVar'
23+
24+
}
25+
26+
variableDeclarationsLetVar();
27+
console.log('after: variableDeclarationsLetVar():', foo);
28+
29+
} catch(err) { console.error(err); }
30+
31+
// `let` is also great when dealing with for-loops
32+
for (let i = 0; i < 2; ++i) {
33+
console.log('I am a loop with let i');
34+
}
35+
36+
// notice the same `let` variable is used in the same scope
37+
for (let i = 0; i < 2; ++i) {
38+
console.log('I am another loop with let i');
39+
}
40+
41+
console.log('-/let-\n');
42+
console.log('-const-');
43+
44+
const amazing_people = ['PatrickJS', 'Lukas', 'Jeff', 'Dan'];
45+
46+
console.log(amazing_people);
47+
48+
amazing_people.push('Douglas Crockford');
49+
amazing_people.push('Brendan Eich');
50+
51+
console.log(amazing_people);
52+
53+
amazing_people = ['Blake Embrey', 'TJ Holowaychuk']; // shouldn't happen
54+
55+
console.log('ref change', amazing_people); // warning happens
56+
57+
console.log('-/const-\n');

Diff for: 01-es6/2.arrow-functions.ts

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* New ES6 Arrow Functions allows us to preserve the context of our callbacks
3+
*
4+
*/
5+
6+
7+
let object = {
8+
collection: ['PatrickJS', 'Lukas', 'Jeff', 'Dan'],
9+
value: 'print some value:',
10+
method: function() {
11+
console.log(this.value, 'method');
12+
this.collection.forEach(function(item) {
13+
console.log(this.value, item);
14+
});
15+
}
16+
};
17+
18+
// notice how this.value inside of the forEach callback is `undefined`
19+
object.method();
20+
21+
22+
let object2 = {
23+
collection: ['PatrickJS', 'Lukas', 'Jeff', 'Dan'],
24+
value: 'print some value:',
25+
method: function() {
26+
console.log(this.value, 'method');
27+
this.collection.forEach((item) => {
28+
console.log(this.value, item);
29+
});
30+
}
31+
};
32+
33+
// we fixed this by preserving the context for the callback
34+
object2.method();
35+
36+
/*
37+
* what's happening is the context of when the function was created is preserved in the function
38+
39+
var object2 = {
40+
collection: ['PatrickJS', 'Lukas', 'Jeff', 'Dan'],
41+
value: 'print some value:',
42+
method: function() {
43+
var _self = this;
44+
console.log(this.value, 'method');
45+
this.collection.forEach(function(item) {
46+
console.log(_self.value, item);
47+
});
48+
}
49+
};
50+
51+
* here's another way to write this in ES5
52+
53+
var object2 = {
54+
collection: ['PatrickJS', 'Lukas', 'Jeff', 'Dan'],
55+
value: 'print some value:',
56+
method: function() {
57+
console.log(this.value, 'method');
58+
this.collection.forEach(function(item) {
59+
console.log(this.value, item);
60+
}.bind(this));
61+
}
62+
};
63+
*/
64+
65+
66+
function callingBack(callback) {
67+
callback();
68+
}
69+
70+
console.log('arrow callback pattern');
71+
try {
72+
73+
callingBack(object2.method); // this doesn't work and throws an error
74+
75+
} catch(e) { console.error(e); }
76+
77+
// very common pattern in order to preserve the context
78+
callingBack(() => object2.method());

Diff for: 01-es6/3.destructuring.ts

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
// ES6
3+
4+
// import {ClassObject} from 'framework/or/library';
5+
6+
// ES5
7+
8+
// var lib = require('framework/or/library');
9+
// var ClassObject = lib.ClassObject;
10+
11+
12+
var { x, y } = { x: 11, y: 8 }; // x = 11; y = 8
13+
console.log(x,y);
14+
15+
16+
// This declaration is equivalent to:
17+
var { x: x, y: y } = { x: 11, y: 8 };
18+
19+
console.log(x,y);
20+
21+
// swap variables in ES5
22+
!function() {
23+
24+
let a = 1;
25+
let b = 2;
26+
27+
console.log(a, b); // 1,2
28+
29+
let temp = b; // hold the value in a temp var
30+
b = a;
31+
a = temp
32+
33+
console.log(a, b); // 2,1
34+
35+
}();
36+
37+
// in ES6 we can use an array as our temp placeholder
38+
!function() {
39+
40+
let a = 1;
41+
let b = 2;
42+
43+
console.log(a, b); // 1,2
44+
45+
[b, a] = [a, b];
46+
47+
console.log(a, b); // 2,1
48+
49+
}();
50+
51+
// now it's look at it in ES5
52+
!function() {
53+
54+
let a = 1;
55+
let b = 2;
56+
57+
console.log(a, b); // 1,2
58+
59+
60+
let temp = [a, b];
61+
b = temp[0];
62+
a = temp[1];
63+
64+
console.log(a, b); // 2,1
65+
66+
}();
67+

Diff for: 01-es6/4.template-string.ts

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* With ES6 we finally have multi-line string support
3+
*/
4+
5+
let prevouly = '\n'+
6+
' <h1>Test</h1>\n'+
7+
' <p>testing</p>\n'+
8+
'';
9+
console.log('template strings prevouly\n', prevouly)
10+
11+
// now we can use back-ticks
12+
13+
let now = `
14+
<h1>Test</h1>
15+
<p>testing</p>
16+
`;
17+
18+
console.log('template strings now\n', now);
19+
20+
// we also get interpolation features
21+
let feature = 'interpolation';
22+
23+
console.log(`testing out the new ${ feature } feature in ES6`)
24+
25+
26+
console.log('---------------------------------------------');
27+
}();
28+
!function() {
29+
console.log('---------------Default Arguments-------------');
30+
/*
31+
* With ES6 we can declare a default argument
32+
*/
33+
34+
function returnText(text = 'default text') {
35+
return 'return:\n' + text;
36+
}
37+
38+
console.log(returnText());
39+
40+
console.log(returnText('now with text'));

Diff for: 01-es6/5.rest-arguments.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* With ES6 we can convert the arguments Object into an array
3+
*/
4+
5+
function printObjects(...objects) {
6+
objects.forEach(obj => {
7+
console.log('ES6 rest object:', obj);
8+
});
9+
}
10+
11+
printObjects({name: 'PatrickJS'}, {name: 'Lukas'}, {name: 'Jeff'}, {name: 'Dan'});
12+
13+
console.log('-es5-');
14+
// in ES5 we would do
15+
function printObjectsES5(objects) {
16+
// re-assign objects as an Array of objects
17+
objects = Array.prototype.slice.call(arguments);
18+
19+
objects.forEach(obj => {
20+
console.log('ES5 rest object:', obj);
21+
});
22+
}
23+
24+
printObjectsES5({name: 'PatrickJS'}, {name: 'Lukas'}, {name: 'Jeff'}, {name: 'Dan'});
25+

Diff for: 01-es6/6.spread-arguments.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* With ES6 spread out an array into arguments for a function
3+
*/
4+
5+
function f(x, y, z) {
6+
return x + y + z;
7+
}
8+
// Pass each elem of array as argument
9+
f(...[1, 2, 3]) === 6
10+
11+
function printObjects(...objects) {
12+
console.log('rest object:', ...objects);
13+
}
14+
15+
printObjects({name: 'PatrickJS'}, {name: 'Lukas'}, {name: 'Jeff'}, {name: 'Dan'});
16+

Diff for: 01-es6/7.class.ts

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* We now have sugar for a common pattern a lot of people use in ES5
3+
* we commented out the code because class declarations needs to be
4+
* declared at the top-level declaration
5+
*/
6+
7+
8+
9+
// ES6 class
10+
class TestObject {
11+
12+
}
13+
14+
console.log('\n', TestObject.toString())
15+
16+
// ES5 we would do this
17+
function TestObjectES5() {
18+
19+
}
20+
21+
console.log('\n', TestObjectES5.toString())
22+
23+
24+
25+
26+
// what happens when we add a method?
27+
28+
// ES6 class
29+
class TestObject {
30+
method() {
31+
console.log('method');
32+
}
33+
}
34+
35+
console.log('\n', TestObject.prototype.method.toString())
36+
37+
// ES5 we would do this
38+
function TestObjectES5() {
39+
40+
}
41+
42+
TestObjectES5.prototype.method = function() {
43+
console.log('method');
44+
}
45+
46+
console.log('\n', TestObjectES5.prototype.method.toString())
47+
48+
49+
50+
// what happens when we subclass?
51+
52+
class AnotherObject {
53+
54+
}
55+
// ES6 class
56+
class TestObject extends AnotherObject {
57+
method() {
58+
console.log('method');
59+
}
60+
}
61+
62+
function AnotherObjectES5() {
63+
64+
}
65+
66+
// ES5 we would do this
67+
function TestObjectES5() {
68+
AnotherObjectES5.apply(this, arguments);
69+
70+
}
71+
72+
TestObjectES5.prototype = Object.create(AnotherObjectES5);
73+
TestObjectES5.prototype.constructor = TestObjectES5;
74+
75+
TestObjectES5.prototype.method = function() {
76+
console.log('method');
77+
}
78+
79+

0 commit comments

Comments
 (0)