javascript - doag.org · 4 history ecmascript 1 (1997): first release ecmascript 2 (1998): minor...

46
1 Slides: JavaScript im Jahr 2017 / Christian Kaltepoth @chkal http://bit.ly/javaland-js2017

Upload: others

Post on 19-Mar-2020

23 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

1

Slides:

JavaScriptim Jahr 2017

/ Christian Kaltepoth @chkal

http://bit.ly/javaland-js2017

Page 2: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

2

Christian KaltepothSenior Developer @ ingenit

/ [email protected] @chkal

http://blog.kaltepoth.de

Page 3: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

3

JavaScriptaka

ECMAScript

Page 4: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

4

HistoryECMAScript 1 (1997): first releaseECMAScript 2 (1998): minor alignmentECMAScript 3 (1999): regex, exceptions, ...ECMAScript 4: killed in 2007ECMAScript 5 (2009): strict mode, JSON, ...ECMAScript 2015: major update!ECMAScript 2016: exp. operator, ...ECMAScript 2017: async functions, ...

Page 5: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

5

Block Scope

Page 6: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

6

ES5 Scopingfunction someFunction() for( var i = 0; i < 4; i++ ) var j = i * i; console.log( j ); // > 9

Page 7: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

7

Immediately-Invoked FunctionExpression (IIFE)

(function() var secret = 42; )(); console.log( secret ); // > ReferenceError: secret is not defined

Page 8: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

8

ES2015 Block Scopefunction someFunction() for( let i = 0; i < 4; i++ ) let j = i * i; console.log( j ); // > ReferenceError: j is not defined

Page 9: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

9

ES2015 Constantsconst users = [ "Christian" ]; users.push( "Jim" ); // > 2 users = [ "Bob" ]; // > SyntaxError: "users" is read­only

Page 10: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

10

Recommendation1. const2. let3. var (ignore)

Page 11: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

11

Arrow Functions

Page 12: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

12

ES5 Functionsvar numbers = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]; numbers.filter( function( n ) return n % 2 !== 0; ); // > [ 1, 3, 5, 7, 9 ]

Page 13: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

13

ES2015 Arrow Functionsnumbers.filter( n => return n % 2 !== 0; ); // > [ 1, 3, 5, 7, 9 ]

numbers.filter( n => n % 2 !== 0 ); // > [ 1, 3, 5, 7, 9 ]

numbers.filter( n => n % 2 ); // > [ 1, 3, 5, 7, 9 ]

Page 14: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

14

Template Strings

Page 15: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

15

ES5 String Concatenationvar name = "Christian"; var count = 213; var message = "Hello " + name + ", you have " + count + " unread messages."; console.log( message );

Page 16: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

16

ES2015 Template Stringsconst name = "Christian"; const count = 213; const message = `Hello $name, you have $count messages.`;

const html = `<h1>Hello $name</h1> <p> You have $count unread messages </p>`;

Page 17: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

17

Collection Types

Page 18: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

18

ES2015 Setsconst tags = new Set(); tags.add( "java" ); tags.add( "javascript" ); tags.add( "java" ); tags.size === 2; // > true tags.has( "java" ); // > true

Page 19: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

19

ES2015 Mapsconst map = new Map(); map.set( "hello", 42 ); map.size === 1; // > true map.get( "hello" ); // > 42 map.delete( "hello" ); // > true

Page 20: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

20

ES5 Iterationvar primes = [ 3, 5, 7, 11, 13 ]; for( var i = 0; i < primes.length; i++ ) console.log( primes[i] ); // ES5 primes.forEach( function( n ) console.log( n ); );

Page 21: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

21

ES2015 for..of// arrays const primes = [ 3, 5, 7, 11, 13 ]; for( let p of primes ) console.log( p ); // collections const set = new Set(); set.add( "foo" ); set.add( "bar" ); for( let s of set ) console.log( s );

Page 22: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

22

Default Parameterfunction formatMoney( value, currency = "$" ) return value.toFixed( 2 ) + currency; formatMoney( 42.99, "€" ); // > 42.99€ formatMoney( 42.99 ); // > 42.99$

Page 23: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

23

Classes

Page 24: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

24

ES5: Constructor Functionsvar Person = function( name ) this.name = name; Person.prototype.greet = function() return "Hello " + this.name; var christian = new Person( "Christian" ); christian.greet(); // > Hello Christian

Page 25: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

25

ES2015 Classesclass Person constructor( name ) this.name = name; greet() return "Hello " + this.name; const christian = new Person( "Christian" ); christian.greet(); // > Hello Christian

Page 26: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

26

ES2015 Inheritanceclass Developer extends Person constructor( name, languages ) super( name ); this.languages = languages; getLanguages() return this.languages.join( ", " ); const christian = new Developer( "Christian", [ "Java", "JavaScript" ] );

Page 27: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

27

Modules

Page 28: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

28

Export / Import// math.js export function max( a, b ) return a > b ? a : b; export const PI = 3.14156;

// foobar.js import max, PI from "./math.js"; max(9, 13) === 13; // > true PI === 3.14156; // > true

Page 29: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

29

Export / Import// math.js export function max( a, b ) return a > b ? a : b; export const PI = 3.14156;

// foobar.js import * as math from "./math.js"; math.max(9, 13) === 13 // > true math.PI === 3.14156 // > true

Page 30: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

30

Promises

Page 31: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

31

Callback HellasyncFunc1( function () asyncFunc2( function () asyncFunc3( function () asyncFunc4( function () // OMG! ); ); ); );

Page 32: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

32

Promiseconst promise = asyncFunc(); promise.then( result => // success (resolved) ); promise.catch( error => // failure (rejected) );

Page 33: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

33

Chaining PromisesasyncFunc1() // Step #1 .then( result1 => return asyncFunc2(); // Step #2 ) .then( result2 => return asyncFunc3(); // Step #3 ) .then( result3 => // handle final result ) .catch( error => // handle all errors );

Page 34: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

34

Creating Promisesfunction asyncFunc()

Page 35: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

35

Creating Promisesfunction asyncFunc() return new Promise( ( resolve, reject ) => );

Page 36: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

36

Creating Promisesfunction asyncFunc() return new Promise( ( resolve, reject ) => setTimeout( () => , 5000); );

Page 37: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

37

Creating Promisesfunction asyncFunc() return new Promise( ( resolve, reject ) => setTimeout( () => resolve( "Promise resolved!" ); , 5000); );

Page 38: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

38

And what aboutES2016 / ES2017?

Page 39: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

39

New in ES2016Math.pow( 3, 2 ); // ES2015 // > 9 3 ** 2 // ES2016 // > 9

const numbers = [ 1, 2, 4, 8 ]; numbers.includes( 2 ); // > true numbers.includes( 3 ); // > false

Page 40: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

40

TC39 ProcessFrequent releases (yearly)Feature stages:

Stage 0: StrawmanStage 1: ProposalStage 2: DraStage 3: CandidateStage 4: Finished

Page 41: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

41

ES2017: Async Functionsfunction asyncFunc() return new Promise( ( resolve, reject ) => setTimeout( () => resolve( "Foobar" ); , 2000); );

async function someFunction() let result = await asyncFunc(); console.log( result ); // > "Foobar"

Page 42: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

42

Can I use this stuff?

Page 43: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

43

ES2015 Compatibility

ES2016 + ES2017 Compatibility

Source: https://kangax.github.io/compat-table/

Page 45: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

45

Java Boilerplatehttps://github.com/chkal/frontend-boilerplate

Apache Mavennode.js / npmWebpack / Babel / TypeScriptKarma / Jasmine

Page 46: JavaScript - doag.org · 4 History ECMAScript 1 (1997): first release ECMAScript 2 (1998): minor alignment ECMAScript 3 (1999): regex, exceptions, ... ECMAScript 4: killed in 2007

46

Thanks!Questions?

http://bit.ly/javaland-js2017https://github.com/chkal/frontend-boilerplate

/ Christian Kaltepoth @chkal