Share
Embed
Build the Future of the Web with Modern JavaScript
This presentation is about what's new in JavaScript, usually referred to as ESNext: new syntax, language features, etc.
Arnelle Balane
July 21, 2018
Transcript
100 slides
-
Build the Future of the Web with Modern JavaScript Build
the Future of the Web with Modern JavaScript Build the Future of the Web with Modern JavaScript Photo by Aditya Vyas on Unsplash -
Module Systems Module Systems CommonJS ★ NodeJS ★ require() AMD
(Asynchronous Module Definition) ★ Browsers ★ RequireJS, etc. -
Module Systems Module Systems CommonJS ★ NodeJS ★ require() AMD
(Asynchronous Module Definition) ★ Browsers ★ RequireJS, etc. UMD (Universal Module Definition) ★ CommonJS + AMD -
Module Systems Module Systems CommonJS ★ NodeJS ★ require() AMD
(Asynchronous Module Definition) ★ Browsers ★ RequireJS, etc. UMD (Universal Module Definition) ★ CommonJS + AMD -
ES Modules ES Modules // lib.mjs export function greet() {
console.log('Hello World!'); } -
ES Modules ES Modules // lib.mjs export function greet() {
console.log('Hello World!'); } // main.mjs import {greet} from './lib.mjs'; greet(); // logs 'Hello World!' -
ES Modules ES Modules // lib.mjs export default function greet()
{ console.log('Hello World!'); } // main.mjs import greet from './lib.mjs'; greet(); // logs 'Hello World!' -
★ import and export not available ★ Evaluated every time
the script is loaded ★ Variables in the top-level scope become global variables ★ Not strict mode by default ★ import and export keywords ★ Evaluated only once ★ Variables in the top-level scope don’t become global variables ★ 'use strict' by default ES Modules ES Modules Regular Scripts Regular Scripts -
★ import and export not available ★ Evaluated every time
the script is loaded ★ Variables in the top-level scope become global variables ★ Not strict mode by default ★ import and export keywords ★ Evaluated only once ★ Variables in the top-level scope don’t become global variables ★ 'use strict' by default ES Modules ES Modules Regular Scripts Regular Scripts -
ES Modules ES Modules Regular Scripts Regular Scripts ★ import
and export keywords ★ Evaluated only once ★ Variables in the top-level scope don’t become global variables ★ 'use strict' by default ★ import and export not available ★ Evaluated every time the script is loaded ★ Variables in the top-level scope become global variables ★ Not strict mode by default -
ES Modules ES Modules Regular Scripts Regular Scripts ★ import
and export keywords ★ Evaluated only once ★ Variables in the top-level scope don’t become global variables ★ 'use strict' by default ★ import and export not available ★ Evaluated every time the script is loaded ★ Variables in the top-level scope become global variables ★ Not strict mode by default -
★ import and export keywords ★ Evaluated only once ★
Variables in the top-level scope don’t become global variables ★ 'use strict' by default ES Modules ES Modules Regular Scripts Regular Scripts ★ import and export not available ★ Evaluated every time the script is loaded ★ Variables in the top-level scope become global variables ★ Not strict mode by default -
★ Consistency with NodeJS ★ “This file contains code that
most likely will not work in regular scripts!” File Extension File Extension <!-- index.html --> <script type="module" src="main.mjs"></script> -
Fallback Fallback <!-- index.html --> <script type="module" src="main.mjs"></script> <script nomodule
src="lib.js"></script> <script nomodule src="main.js"></script> -
Additional Behavior Additional Behavior ★ “Bare” module specifiers are not
supported ★ Deferred by default, even for inline modules ★ Always fetched with CORS ❌ import greet from 'greet.mjs'; ✅ import greet from '/greet.mjs'; ✅ import greet from './greet.mjs'; ✅ import greet from '../greet.mjs'; ✅ import greet from 'https://example.com/greet.mjs'; -
import() Function import() Function // main.mjs import('./greet.mjs') .then(greet => greet());
// main.mjs const greet = await import('./greet.mjs') greet(); -
★ Can load modules on-demand ★ Can be called anywhere
in the code ★ Can be used inside classic scripts ★ Can be statically analyzed (helpful for build tools) ★ Can only be used at the top-level scope of a file ★ Can only be used inside module scripts Static Imports Static Imports Dynamic Imports Dynamic Imports -
★ Can load modules on-demand ★ Can be called anywhere
in the code ★ Can be used inside classic scripts ★ Can be statically analyzed (helpful for build tools) ★ Can only be used at the top-level scope of a file ★ Can only be used inside module scripts Static Imports Static Imports Dynamic Imports Dynamic Imports -
Static Imports Static Imports Dynamic Imports Dynamic Imports ★ Can
be statically analyzed (helpful for build tools) ★ Can only be used at the top-level scope of a file ★ Can only be used inside module scripts ★ Can load modules on-demand ★ Can be called anywhere in the code ★ Can be used inside classic scripts -
source: https://gist.github.com/ebidel/3201b36f59f26525eb606663f7b487d0 Feature Detection Feature Detection function supportsDynamicImport() { try
{ new Function('import("")'); return true; } catch (err) { return false; } } -
Numbers Numbers ★ Represented as double-precision floats ★ -(253 -
1) to 253 - 1 ★ Integers and floating-point numbers -
BigInt ★ New numeric primitive for arbitrary-precision integers ★ Store
and operate on very large integers BigInt -
BigInt ★ New numeric primitive for arbitrary-precision integers ★ Store
and operate on very large integers BigInt const eleven = 11n; const twelve = BigInt(12); typeof eleven === 'bigint'; // true -
12n + 12n; // 24n 12n * 2; // TypeError
12n * BigInt(2); // 24n BigInt BigInt -
BigInt BigInt 12n + 12n; // 24n 12n * 2;
// TypeError 12n * BigInt(2); // 24n const googol = 10n ** 100n; // 100000000000000000000000000000000000000000000000000 // 00000000000000000000000000000000000000000000000000n -
Photo by Perchek Industrie on Unsplash New Promise Methods New
Promise Methods New Promise Methods -
finally() finally() ★ Executes a callback when the Promise gets
either resolved or rejected -
finally() finally() ★ Executes a callback when the Promise gets
either resolved or rejected performAction() .then(result => { console.log(result); isDone = true; }) .catch(error => { console.error(error); isDone = true; }); -
finally() finally() ★ Executes a callback when the Promise gets
either resolved or rejected performAction() .then(result => { console.log(result); isDone = true; }) .catch(error => { console.error(error); isDone = true; }); -
finally() finally() ★ Executes a callback when the Promise gets
either resolved or rejected performAction() .then(console.log) .catch(console.error) .finally(() => isDone = true); performAction() .then(result => { console.log(result); isDone = true; }) .catch(error => { console.error(error); isDone = true; }); -
finally() finally() ★ Executes a callback when the Promise gets
either resolved or rejected performAction() .then(result => { console.log(result); isDone = true; }) .catch(error => { console.error(error); isDone = true; }); performAction() .then(console.log) .catch(console.error) .finally(() => isDone = true); -
Photo by Mitch Meyers on Unsplash New RegExp Features New
RegExp Features New RegExp Features -
const input = '2018-07'; const pattern = /^(\d{4})-(\d{2})$/; const matched
= input.match(pattern); const year = matched[1]; const month = matched[2]; Named Capture Groups Named Capture Groups Capture Groups -
const input = '2018-07'; const pattern = /^(?<year>\d{4})-(?<month>\d{2})$/; const matched
= input.match(pattern); const year = matched.groups.year; const month = matched.groups.month; Named Capture Groups Named Capture Groups Named Capture Groups -
const input = '2018-07'; const pattern = /^(?<year>\d{4})-(?<month>\d{2})$/; const matched
= input.match(pattern); const year = matched.groups.year; const month = matched.groups.month; const {year, month} = matched.groups; Named Capture Groups Named Capture Groups Named Capture Groups -
const input = '@arnellebalane, @channelfix hello there!'; const pattern =
/@[\w\d.]+/ig; Lookbehind Assertions Lookbehind Assertions “Get all mentioned usernames, without the @” -
const input = '@arnellebalane, @channelfix hello there!'; const pattern =
/@[\w\d.]+/ig; Lookbehind Assertions Lookbehind Assertions “Get all mentioned usernames, without the @” -
const input = '@arnellebalane, @channelfix hello there!'; const pattern =
/@[\w\d.]+/ig; Lookbehind Assertions Lookbehind Assertions “Get all mentioned usernames, without the @” const input = '@arnellebalane, @channelfix hello there!'; const pattern = /(?<=@)[\w\d.]+/ig; -
const input = '@arnellebalane, @channelfix hello there!'; const pattern =
/@[\w\d.]+/ig; Lookbehind Assertions Lookbehind Assertions “Get all mentioned usernames, without the @” const input = '@arnellebalane, @channelfix hello there!'; const pattern = /(?<=@)[\w\d.]+/ig; -
const input = '@arnellebalane, @channelfix hello there!'; const pattern =
/@[\w\d.]+/ig; Lookbehind Assertions Lookbehind Assertions “Get all mentioned usernames, without the @” const input = '@arnellebalane, @channelfix hello there!'; const pattern = /(?<!@)[\w\d.]+/ig; -
Class Field Declarations Class Field Declarations Class Field Declarations Photo
by Annie Spratt on Unsplash -
Class Fields Class Fields class Person { constructor(name) { this.name
= name; this.isAlive = true; } kill() { this.isAlive = false; } } -
class Person { constructor(name) { this.name = name; this.isAlive =
true; } kill() { this.isAlive = false; } } Class Fields Class Fields -
class Person { constructor(name) { this.name = name; this.isAlive =
true; } kill() { this.isAlive = false; } } class Person { isAlive = true; constructor(name) { this.name = name; } kill() { this.isAlive = false; } } Class Fields Class Fields -
class Person { constructor(name) { this.name = name; this.isAlive =
true; } kill() { this.isAlive = false; } } class Person { isAlive = true; constructor(name) { this.name = name; } kill() { this.isAlive = false; } } Class Fields Class Fields -
Photo by Joshua Newton on Unsplash Private Fields and Methods
Private Fields and Methods Private Fields and Methods -
Private Fields Private Fields class Person { isAlive = true;
kill() { this.isAlive = false; } } -
Private Fields Private Fields class Person { isAlive = true;
kill() { this.isAlive = false; } } const me = new Person(); me.isAlive = false; // -
Private Fields Private Fields const me = new Person(); me.isAlive
= false; // class Person { #isAlive = true; kill() { this.#isAlive = false; } } -
Private Fields Private Fields const me = new Person(); me.isAlive
= false; // class Person { #isAlive = true; kill() { this.#isAlive = false; } } -
Private Fields Private Fields const me = new Person(); me.isAlive
= false; // undefined me.#isAlive = false; // SyntaxError class Person { #isAlive = true; kill() { this.#isAlive = false; } } -
class Person { #isAlive = true; kill() { this.#isAlive =
false; } } const me = new Person(); me.kill(); // Private Methods Private Methods -
Private Methods Private Methods const me = new Person(); me.kill();
// class Person { #isAlive = true; #kill() { this.#isAlive = false; } } -
const me = new Person(); me.kill(); // class Person {
#isAlive = true; #kill() { this.#isAlive = false; } } Private Methods Private Methods -
const me = new Person(); me.#kill(); // SyntaxError class Person
{ #isAlive = true; #kill() { this.#isAlive = false; } } Private Methods Private Methods -
Photo by Echo Grid on Unsplash Static Class Properties Static
Class Properties Static Class Properties -
Static Properties Static Properties const me = new Person(); me.isAlive;
// true class Person { isAlive = true; } -
class Person { static isAwesome = true; isAlive = true;
} const me = new Person(); me.isAlive; // true Person.isAwesome; // true Static Properties Static Properties -
★ Technical Committee number 39 ★ Part of ECMA International,
the institution that standardizes the JavaScript language TC39 TC39 -
★ Technical Committee number 39 ★ Part of ECMA International,
the institution that standardizes the JavaScript language ★ Follows a process based on maturity stages TC39 TC39 -
Stage 0 : strawman Stage 0 : strawman ★ Any
submitted ideas for features to include in the specification ★ Only members of TC39 can create these proposals -
Stage 1 : proposal Stage 1 : proposal ★ A
formal proposal of the feature ★ Championed by a member of TC39 ★ Problem is clearly identified ★ Example solutions and API ★ Discussion of semantics, algorithms, and potential obstacles -
★ A first version of the specification ★ Formal description
of the syntax and semantics of the feature Stage 2 : draft Stage 2 : draft -
★ Mostly finished, now needs feedback from implementations and users
★ Specification text is complete Stage 3 : candidate Stage 3 : candidate -
★ Ready to be included in the standard Stage 4
: finished Stage 4 : finished -
Thank you! Build the Future of the Web with Modern
JavaScript Arnelle Balane Frontend Developer, ChannelFix -
Thank you! Build the Future of the Web with Modern
JavaScript Arnelle Balane Frontend Developer, ChannelFix slides.arnelle.me/iox18