KEMBAR78
Workshop JavaScript ES6+ | PPTX
Intro to Javascript ES6/7/8
WeWork Amsterdam
21/06/2018
Roy Derks
Technical Founder & CTO, SwitchBay
@gethackteam
#javascriptEverywhere
What is ES?
ES is short for ECMAScript
ā€œECMAScript is a
standard.ā€
ā€œECMAScript is
standardised JavaScript.ā€
ā€œJavaScript is a dialect of
ECMAScript.ā€
How does the Versioning work?
Released in 2015
ES6 = ECMAScript 6
The 6th version of ECMAScript
ES2015 = ?
? = ECMAScript 2016
ES8 = ?
2015? Why is ES6 still ā€œnewā€?
ā€œDepends on how (or where)
you use JavaScript.ā€
Short: Browser Support
Also on CodePen
http://bit.ly/es678workshop
Also on CodePen
Contents of this Workshop
• Importing Modules
• Arrow Functions
• Spread / Rest Operators
• Classes
• Let & Constants
• Template Literals
• Object Literals
• Destructuring
Let & Constants
var = XX
let = XX
const = XX
// ā€œimmutatable variableā€
http://bit.ly/es678workshop-1
Template Literals
var sayHi = 'Hi, my name is ' + variable
http://bit.ly/es678workshop-2
const sayHi = `Hi, my name is ${variable}`
Object Literals
http://bit.ly/es678workshop-3
Quickly create objects with properties
inside curly braces
• Short Syntax for Initialising Properties
• Short Syntax for Defining Functions
• Computed Names for Properties
Destructuring
http://bit.ly/es678workshop-4
const { a, b, c } = alphabet
var a = alphabet.a, b = alphabet.b, …
Arrow Functions
http://bit.ly/es678workshop-5
const doThis = (variable) => { … }
function doThis(variable) { return … }
Spread Operator
http://bit.ly/es678workshop-6
Combine or Spread elements into
collections (Arrays and Objects)
const params = [ā€˜param2’, ā€˜param3’]
const combined = [ ā€˜param1’, …params ]
Rest Parameter
http://bit.ly/es678workshop-7
Combine remaining parameters into one
single parameter
const doThis = (param1, …params) => { … }
Classes
http://bit.ly/es678workshop-8
class A {
constructor() { … }
doThis = () => { …}
}
Easily create Classes with Inheritance
class B extends A {
doThis() { …}
}
Iterables / Iterators
http://bit.ly/es678workshop-9
const iterable { [Symbol.iterator](): iterator { …} }
next() {
value: …,
done: true | false
}
Remember? Callback Hell
http://callbackhell.com/
doThis(param, function(err, res) {
doThis(param, function(err, res) {
doThis(param, function(err, res) {
// Do Something…
})
})
}) })
Solution? Promises
new Promise(resolve => {
doThis(param).then(a => {
doThis(param).then(b => {
// Do Something…
})
})
}) })
Async / Await
http://bit.ly/es678workshop-10
async function doThis() {
const waitForThis = await alsoDoThis()
return waitForThis
}
From ā€œCallback Hellā€ to Promises to
Asynchronous
Want to know more?
http://es6-features.org
@gethackteam #javascriptEverywhere

Workshop JavaScript ES6+

Editor's Notes

  • #4Ā ECMAScript is a 100+ page trademarked specification of how JavaScript should (or could) be used. It’s like QWERTY is for keyboards, although you could use AZERTY..
  • #5Ā Question: How would ES7 be called? How would ES8 be called?
  • #6Ā Question: Who of you uses JavaScript on the Frontend? Backend? Both? IE11 still doesn’t support ES6 (without compiler)
  • #10Ā http://bit.ly/es678workshop-1 Also known as "immutable variables" → cannot be re-assigned new content, although the assigned content can be changed. Generally const is used as replacement for var, use let if you want to have the same effects as var. Show that changing the value of ā€˜variable’ replaces the initial value, while changing the value of ā€˜constant’ does nothing. However, the assigned content ā€˜key’ of ā€˜constant’ can be changed. → Show let as a replacer of var
  • #11Ā http://bit.ly/es678workshop-2 Sometimes also called ā€œtemplate stringsā€ So we replaced var with const for now.
  • #12Ā http://bit.ly/es678workshop-3 Object literals make it easy to quickly create objects with properties inside the curly braces. To create an object, we simply notate a list of key: value pairs delimited by comma. ES6 makes the declaring of object literals concise and thus easier. Three major ways it does this are : - Shorthand Syntax for initialising Properties Show we can leave out the property name - Shorthand Syntax for defining Functions Show we can leave out the definition of the function - Computed Names for Properties Show we can use a variable as property name, but can lead to overwriting the name if used twice. So ā€˜Bond’ will overwrite ā€˜James → Add count
  • #13Ā http://bit.ly/es678workshop-4 With ES6 it becomes easier to destructure Arrays and Objects, also within Functions. Show how we can more easily destruct Arrays, Objects (incl. renaming) and apply these methods to functions. You can even do this for multi-level objects!!
  • #14Ā http://bit.ly/es678workshop-5 Cleaner syntax for writing functions Show how we can use an Arrow Function for a simple .map() over an Array of numbers (also use new function from ES6 to more easy create an Array of numbers). Hint: Remember the function we used before to get a Person? We can also write it as a Constant using Arrow Functions.
  • #15Ā http://bit.ly/es678workshop-6 Spread combine arrays and objects into one, show that for combining objects the keys should have unique names.
  • #16Ā http://bit.ly/es678workshop-7 Combine remaining parameters into one single parameter Reuse previous function to show parameters can be combined.
  • #17Ā http://bit.ly/es678workshop-8 Writing Classes in ES5 was a real trouble and had no real support (insert code snippet example). ES6 supports a more intuitive OOP-style with Class Inheritance Fat Arrow Functions don’t work in SubClasses!!!!!! Show how we can convert a function to a Class and even extend it. Also show Spread Operators.
  • #18Ā http://bit.ly/es678workshop-9 AnĀ interableĀ must be an object with a function iterator whose key isĀ  AnĀ iteratorĀ must be an object with a function namedĀ nextĀ that returns an object with the keys:Ā valueā€Šā€”ā€Šthe current item in the iteration; andĀ done— trueĀ if the iteration has finished,Ā falseĀ otherwise. We can also build own iterables -> An object becomes anĀ iterableĀ if it implements a function whose key isĀ Symbol.iteratorĀ and returns anĀ iterator.
  • #19Ā Callbacks: Execute function once asynchronous call returns value. Program doesn’t have to halt and wait for value. Every function has its own Error handling
  • #20Ā Promises: Alleviate ā€œcallback hellā€ A pending promise can either be fulfilled or rejected, at which point the appropriate handler is called viaĀ .then()Ā for successes andĀ .catch()Ā for errors. The main thing to keep in mind: AĀ promiseĀ represents theĀ eventualĀ result of an asynchronous operation. Allows you to write code that assumes a value is returned within a success function Only needs a single error handler
  • #21Ā http://bit.ly/es678workshop-10 Async/Await —> Introduced in ES2017. Allows people to write async code as if it were synchronous AWAIT -> This will pause theĀ asyncĀ function and wait for the Promise to resolve prior to moving on.