catching up with javascript · import *asmod1 from"../lib/module1"; mod1.hello();...
TRANSCRIPT
![Page 1: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/1.jpg)
Catching upwith JavaScript– ES 2015 edition
![Page 2: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/2.jpg)
JavaScript ECMAScript 2015
History of JavaScript
201520051995
ECMAScript 2
ECMAScript 3
ECMAScript 5
ECMAScript 1
![Page 3: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/3.jpg)
Problem 1
• How do we organize and structure our code?
![Page 4: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/4.jpg)
'use strict';var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { vargetter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeofcall === "function") ? call : self; }function _inherits(subClass, superClass) { if (typeofsuperClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }var Volkswagen = (function (_Car) { _inherits(Volkswagen, _Car); function Volkswagen(year, model) { _classCallCheck(this, Volkswagen); var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Volkswagen).call(this, year, model)); _this.origin = 'Germany'; return _this; } _createClass(Volkswagen, [{ key: 'getEmission', value: function getEmission() { var emission = _get(Object.getPrototypeOf(Volkswagen.prototype), 'getEmission', this).call(this); return this.isTestRunning ? emission / 2 : emission; } }]); return Volkswagen;})(Car);
Classes
class Volkswagen extends Car {constructor(model, isTestRunning) {super(model);this.isTestRunning = isTestRunning;
}getEmission() {var emission = super.getEmission();return this.isTestRunning ?emission / 2 : emission;
}static getOrigin() { return "Germany"; }
}
ES5ES 2015
![Page 5: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/5.jpg)
{"presets": ["es2015"]
}
Babel
• A transpiler for JavaScript– ES 2015, ES 2016, JSX…
– You choose which transformsto apply using plugins– Presets for common uses
• Other tools exists– But not really... > npm install babel-cli
> npm install babel-preset-es2015
> ./node_modules/.bin/babel script.js
.babelrc
![Page 6: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/6.jpg)
• In JavaScript the default way is to include and structurecode manually using <script>
• Other solutions are available
– CommonJS (Node.js)– Asynchronous Module Definition (RequireJS)– Universal Module Definition
Modules
![Page 7: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/7.jpg)
import * as mod1 from "../lib/module1";mod1.hello();mod1.hello() === say.helloPhrasevar car = new mod1.Volkswagen()
import { hello, helloPhrase, Volkswagen } from "../lib/module1";
hello();hello() === helloString;var car = new Volksvagen();
Modules
export function hello() {console.log("Hello world");
};export var helloPhrase = "Hello world";export class Volkswagen { … }
app/main.jslib/module1.js
![Page 8: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/8.jpg)
import * as mod1 from "../lib/module1";mod1.hello();mod1.hello() === say.helloPhrasevar car = new mod1.Volkswagen()
import { hello, helloPhrase, Volkswagen } from "../lib/module1";
hello();hello() === helloString;var car = new Volksvagen();
Modules
function hello() {console.log("Hello world");
};var helloPhrase = "Hello world";class Volkswagen { … }
export {hello,helloPhrase,Volkswagen
};
app/main.jslib/module1.js
![Page 9: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/9.jpg)
import Volkswagen from "../lib/module1";var car = new Volksvagen();
import { hello, helloPhrase }from "../lib/module1";
hello();hello() === helloString;
Modules
export default class Volkswagen { … }
export function hello() {console.log("Hello world");
};export var helloPhrase = "Hello world";
app/main.jslib/module1.js
![Page 10: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/10.jpg)
module.exports = {entry: "./app/main.js",output: {path: "./dist",filename: "app.js"
},module: {loaders: [{test: /\.js$/,loader: "babel"
}]}
};
Webpack
• Bundles modules and itsdependencies as static assets
– JavaScript– CSS, HTML, Images, …
• Complex but worth it– Server, Hot Reloading,
Sourcemaps, ...
Webpack.config.js
> npm install webpack> ./node_modules/.bin/webpack
![Page 11: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/11.jpg)
Problem 2
• Why is JavaScript such a crappy language?
![Page 12: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/12.jpg)
var msg;var i;var txt = ["a", "b", "c"];for (i = 0; i < 3; i++) {msg = txt[i];setTimeout(function () {console.log(msg);
}, 1000);}
Managing scope
var txt = ["a", "b", "c"];for (var i = 0; i < 3; i++) {var msg = txt[i];setTimeout(function () {console.log(msg);
}, 1000);}
Actualchallenge1.js
> node challenge1.jsc c c
![Page 13: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/13.jpg)
var testVar = "I'm a global";function challenge2 () {var testVar;alert(testVar);testVar = "I'm a local var";alert(testVar);
}challenge2();
Managing scope
var testVar = "I'm a global";function challenge2 () {alert(testVar);var testVar = "I'm a local var";alert(testVar);
}challenge2();
Actualchallenge2.js
> node challenge2.jsundefinedI'm a local var
![Page 14: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/14.jpg)
Managing scope
let testVar = "I'm a global";function challenge2 () {alert(testVar);let testVar = "I'm a local var";alert(testVar);
}challenge2();
challenge2.js
> node challenge2.jsReferenceError: testVar is not defined
![Page 15: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/15.jpg)
Managing scope
let txt = ["a", "b", "c"];for (let i = 0; i < 3; i++) {let msg = txt[i];setTimeout(function () {console.log(msg);
}, 1000);}
challenge1.js
> node challenge1.jsa b c
![Page 16: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/16.jpg)
const name = "kokitotsos";name = "kits";TypeError: Assignment to constant variable.
const name = {name: "kokitotsos"
};name.name = "kits";
Managing scope
• Use let instead of var• Use const if you can
– The reference is contant– The content can still change– Use deepFreeze to freeze
content
![Page 17: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/17.jpg)
{"parser": "babel-eslint”,"parserOptions": {"ecmaVersion": 6,"sourceType": "module”
},"rules": {"no-var": 2
}}
ESLint
• Should be present in allJavaScript projects
– Used to find errors in your code– Lots of pluggable rules– Special rules for frameworks
• Has replaced JSHint
.eslintrc
> npm install eslint> npm install babel-eslint
> ./node_modules/.bin/eslint main.js
1:1 error Unexpected var, use let or constinstead no-var
![Page 18: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/18.jpg)
What is this?
class Volkswagen {constructor(model) {this.model = model;
}printModel() {console.log(this.model);
}}
(new Volkswagen("Golf")).printModel();
challenge3.js> node challenge3.jsGolf
![Page 19: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/19.jpg)
What is this?
class Volkswagen {constructor(model) {this.model = model;
}printModel() {setTimeout(function () {console.log(this.model);
}, 0);}
}
(new Volkswagen("Golf")).printModel();
challenge3.js> node challenge3.jsundefined
![Page 20: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/20.jpg)
• Shorthand syntax for functions• Share the same lexical this
as surrounding code
const hello = () => {console.log("hi");
}const add = (a, b) => {return a + b;
}const square = x => x * x;
Arrow functions
![Page 21: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/21.jpg)
Arrow functions
class Volkswagen {constructor(model) {this.model = model;
}printModel() {setTimeout(function () {console.log(this.model);
}, 0);}
}
(new Volkswagen("Golf")).printModel();
challenge3.js> node challenge3.js
![Page 22: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/22.jpg)
Arrow functions
class Volkswagen {constructor(model) {this.model = model;
}printModel() {setTimeout(() => {console.log(this.model);
}, 0);}
}
(new Volkswagen("Golf")).printModel();
challenge3.js> node challenge3.jsGolf
![Page 23: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/23.jpg)
var username = props.username;var show = props.show;
Destructuring
const props = {username: "kokitotsos",show: true
};
ES 5
const { username, show } = props;
ES 2015
![Page 24: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/24.jpg)
const {a: {b,
d: [, f, g]}
} = obj;
Destructuring
const obj = {a: {b: 1,c: 2,d: [3, 4, 5]
}};
ES 2015
![Page 25: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/25.jpg)
• Template litterals const a = `string with expressions ${1+2}`;
More features
![Page 26: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/26.jpg)
• Template litterals• Default parameters
const sayHiTo = (who = "world") => { console.log(`Hello ${who}`)
}
More features
![Page 27: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/27.jpg)
• Template litterals• Default parameters• Spread operator
const a = [1, 2, 3];const b = [...a, 4, 5] -> [1, 2, 3, 4, 5]
More features
![Page 28: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/28.jpg)
• Template litterals• Default parameters• Spread operator• Rest operator
const d = (e, f, ...args) => {console.log([e, f, ...args]);
}d(1, 2, 3, 4, 5); -> [1, 2, 3, 4, 5]
More features
![Page 29: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/29.jpg)
• Template litterals• Default parameters• Spread operator• Rest operator• Promises
const timeout = duration => new Promise((resolve, reject) =>setTimeout(resolve, duration));
timeout(1000).then(() => timeout(2000)).then(() => throw new Error("hmm")).then(() => timeout(3000)).catch(err =>Promise.all([timeout(100),timeout(200)
]);
More features
![Page 30: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/30.jpg)
• Template litterals• Default parameters• Spread operator• Rest operator• Promises• Generators
function* generator(i) {yield i;yield i + 10;
}const gen = generator(10);console.log(gen.next().value); -> 10console.log(gen.next().value); -> 20
More features
![Page 31: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/31.jpg)
ECMAScript 2016 & 2017
• Aiming for one new release every year• A process for features
• All stages can be enabled in Babel• Decorators and async functions are interesting
0: Strawman 1: Proposal 2: Draft 3: Candidate 4: Finished
![Page 32: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/32.jpg)
Architecture
• How do we build our applications?
![Page 33: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/33.jpg)
JavaScript ECMAScript 2015
History of JavaScript
201520051995
ECMAScript 2
AJAX
ECMAScript 3 MVC
ECMAScript 5
ECMAScript 1
![Page 34: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/34.jpg)
Problem with MVC
• The model is too easy to modify
– Two-way binding demos really well– The model is not always clear– The model can be changed from multiple places
• The controllers and views tend to grow
– Really hard to maintain– Really hard to test
![Page 35: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/35.jpg)
Components
• All frameworks move towards components• A component should…
…be small…do one isolated task…not have side effects if possible
![Page 36: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/36.jpg)
describe("<FrameworkList />", () => {it("renders three frameworks", () => {const frameworks = ["react", "ember", "angular"];
const wrapper = shallow(<FrameworkListframeworks={ frameworks } />
);expect(wrapper.find(li)).to.have.length(3);
});});
Components
const FrameworkList = ({ frameworks = [] }) => (<ul>{ frameworks.map(framework => {
return <li>{ framework }</li>}
}</ul>
);
export default FrameworkList;
frameworklist-spec.jsframeworklist.js
![Page 37: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/37.jpg)
{..."scripts": {"prepublish": "webpack","start": "webpack-dev-server","test": "mocha –-compilers \
js:babel-register"}
}
package.json• Really important for JavaScript• Lots of different tools but these
are objectively the best
– Mocha: Test runner– Chai: Assertions– Sinon: Spies– Enzyme: Component testing
Testing
> npm test -- watch<FrameworkList>
✓ renders all frameworks
![Page 38: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/38.jpg)
Unidirectional dataflow
Reducer
Store
Component
Action
Middleware
API
![Page 39: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/39.jpg)
Unidirectional dataflow
Reducer
Store
Component
Action
Middleware
API
<Button onClick={ handleClick }>Add Todo
</Button>
const handleClick = () => {dispatch({type: "ADD_TODO",text: "Try Redux"
});}
![Page 40: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/40.jpg)
Unidirectional dataflow
Reducer
Store
Component
Action
Middleware
API
{type: "ADD_TODO",text: "Try Redux"
}
![Page 41: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/41.jpg)
Unidirectional dataflow
Reducer
Store
Component
Action
Middleware
API
const initial = {todos: []
};const todoApp = (state = initial, action) => {switch (action.type) {case "ADD_TODO": {return Object.assign({}, state, {todos: [...state.todos,action.text
});}default: return state;
}}
![Page 42: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/42.jpg)
Unidirectional dataflow
Reducer
Store
Component
Action
Middleware
API
import { createStore } from "redux";import { todoApp } from "./reducers";
const store = createStore(todoApp);
![Page 43: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/43.jpg)
Unidirectional dataflow
Reducer
Store
Component
Action
Middleware
API
<Button onClick={ handleClick }>Add Todo
</Button>
const handleClick = () => {store.dispatch({type: "ADD_TODO",text: "Try Redux"
});}
store.subscribe(() => {store.getState();// Update component
});
![Page 44: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/44.jpg)
Unidirectional dataflow
Reducer
Store
Component
Action
Middleware
API
![Page 45: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/45.jpg)
What is missing?
Reducer
Store
Component
Action
Middleware
API
![Page 46: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/46.jpg)
Routing
Reducer
Store
Component
Action
Middleware
API
Router
![Page 47: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/47.jpg)
Frameworks
• Which framework shall we use?
![Page 48: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/48.jpg)
Frameworks
React
�Is leading the way�Huge community�Mature
�Weak architecture� Tool fatigue
Angular 2
�Rethinking Angular�Components�TypeScript
� Immature� I know Angular?
Ember
�Strict architecture�Mature�Productive
� No momentum� Handlebars
![Page 49: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/49.jpg)
Summary
• JavaScript is growing up
– The language is getting better– The tools are really great– Best practices are starting to emerge
• We need to grow up
– Don’t focus on the frameworks– New frameworks pop up every day– Pick one and learn it really well
![Page 50: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/50.jpg)
Thank you forcoming
![Page 51: Catching up with JavaScript · import *asmod1 from"../lib/module1"; mod1.hello(); mod1.hello() === say.helloPhrase varcar = newmod1.Volkswagen() import{ hello, helloPhrase, Volkswagen](https://reader033.vdocuments.site/reader033/viewer/2022042308/5ed414cf8d46b66d22636ce4/html5/thumbnails/51.jpg)
Links
• ECMAScript– https://github.com/lukehoban/
es6features– https://github.com/tc39/
ecma262
• Tools– http://babeljs.io– https://webpack.github.io– http://eslint.org
• Redux– http://redux.js.org
• Frameworks– http://facebook.github.io/react– http://emberjs.com– https://angular.io
• Test– http://mochajs.org– http://chaijs.com– http://sinonjs.org– https://github.com/airbnb/
enzyme