├── 01-es6 ├── 1.variable-declarations.ts ├── 2.arrow-functions.ts ├── 3.destructuring.ts ├── 4.template-string.ts ├── 5.rest-arguments.ts ├── 6.spread-arguments.ts ├── 7.class.ts ├── 8.enhanced-object-literals.ts └── bootstrap.ts ├── 02-es7 ├── 1.decorators.ts └── bootstrap.ts ├── 03-typescript └── bootstrap.ts ├── 04-bootstrapping-angular └── bootstrap.ts ├── 05-components └── bootstrap.ts ├── 06-directives └── bootstrap.ts ├── 07-template-syntax └── bootstrap.ts ├── 13-pipes └── bootstrap.ts ├── 14-forms └── bootstrap.ts ├── README.md ├── game-tictactoe ├── app.ts ├── bootstrap.ts ├── components │ ├── board │ │ ├── board.css │ │ └── board.ts │ └── tictactoe.ts └── services │ └── GameService.ts ├── index.html ├── large-app ├── bootstrap.ts ├── components │ ├── app.css │ ├── app.ts │ ├── dashboard.ts │ └── rxjs_examples │ │ ├── rxjs-examples.ts │ │ └── rxjs.css ├── directives │ ├── Autofocus.ts │ └── directives.ts ├── pipes │ ├── CapitalizePipe.spec.ts │ ├── CapitalizePipe.ts │ ├── RxPipe.ts │ └── pipes.ts └── services │ ├── example-service.ts │ └── services.ts ├── rx-autosuggest ├── app.ts ├── bootstrap.ts ├── components │ └── search-github.ts ├── directives │ └── ac-autosuggest.ts └── services │ └── GithubService.ts ├── rx-draggable ├── app.ts ├── bootstrap.ts ├── components │ └── drag-element.ts └── directives │ └── draggable.ts ├── rx-timeflies ├── app.ts ├── bootstrap.ts ├── components │ └── timeflies.ts └── services │ └── Message.ts ├── server ├── db.js ├── express-server-example.js └── todo_api.js ├── simple-component ├── app.ts ├── bootstrap.ts └── home │ ├── home.css │ ├── home.html │ ├── home.spec.ts │ └── home.ts ├── simple-todo ├── app.ts ├── bootstrap.ts ├── components │ └── todo.ts └── services │ └── TodoService.ts └── webpack.config.js /01-es6/1.variable-declarations.ts: -------------------------------------------------------------------------------- 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'); 58 | -------------------------------------------------------------------------------- /01-es6/2.arrow-functions.ts: -------------------------------------------------------------------------------- 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()); 79 | -------------------------------------------------------------------------------- /01-es6/3.destructuring.ts: -------------------------------------------------------------------------------- 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 | 68 | -------------------------------------------------------------------------------- /01-es6/4.template-string.ts: -------------------------------------------------------------------------------- 1 | /* 2 | * With ES6 we finally have multi-line string support 3 | */ 4 | 5 | let prevouly = '\n'+ 6 | '
testing
\n'+ 8 | ''; 9 | console.log('template strings prevouly\n', prevouly) 10 | 11 | // now we can use back-ticks 12 | 13 | let now = ` 14 |testing
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')); 41 | -------------------------------------------------------------------------------- /01-es6/5.rest-arguments.ts: -------------------------------------------------------------------------------- 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 | 26 | -------------------------------------------------------------------------------- /01-es6/6.spread-arguments.ts: -------------------------------------------------------------------------------- 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 | 17 | -------------------------------------------------------------------------------- /01-es6/7.class.ts: -------------------------------------------------------------------------------- 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 | 80 | -------------------------------------------------------------------------------- /01-es6/8.enhanced-object-literals.ts: -------------------------------------------------------------------------------- 1 | 2 | function handler() { 3 | return 'handler'; 4 | } 5 | function get42() { 6 | return 42; 7 | } 8 | 9 | let object = { 10 | // Shorthand for ‘handler: handler’ 11 | handler, 12 | // Computed (dynamic) property names 13 | [ 'prop_' + get42() ]: 42 14 | }; 15 | 16 | console.log(object.handler()) 17 | console.log(object.prop_42()); 18 | 19 | 20 | /* 21 | 22 | function middleware(req, res) { 23 | db.find(req.params.id).then(data => { 24 | var jsonResponse = { 25 | ...data, 26 | }; 27 | jsonResponse[req.params.id] = Number(req.params.id); 28 | res.json(jsonResponse); 29 | 30 | }); 31 | } 32 | 33 | app.use('/api', middleware); 34 | 35 | function middleware2(req, res) { 36 | db.find(req.params.id).then(data => { 37 | 38 | res.json({ 39 | ...data, 40 | [req.params.id] = Number(req.params.id) 41 | }); 42 | 43 | }); 44 | } 45 | 46 | app.use('/api2', middleware2); 47 | 48 | */ 49 | -------------------------------------------------------------------------------- /01-es6/bootstrap.ts: -------------------------------------------------------------------------------- 1 | var app = ({{ value | capitalize }}
25 |{{ date | date:'mediumDate' }}
26 |{{ grade | percent:'.2' }}
27 |