6 - Advanced JavaScript.
Speck Academy 2021
Objects
Callback functions
Promises
ES6+ features
1
2
3
4
Overview
1
Objects
Objects
JavaScript is designed on a simple object-based paradigm.
Objects in JavaScript, just as in many other programming languages, can be compared to objects in real life.
Objects
In JavaScript there are few popular ways of creating objects, and each has its own special features:
Objects
Object is created with object literals notation:
Objects
Objects
Object is created with constructor function:
Objects
Objects
Difference between method that is stored inside prototype object and one that is attached directly to their object.
Objects
Object is created with class syntactic sugar:
2
Callback functions
Callback functions
Let’s remember what we said last time about the callback functions:
Callback functions
Result of code from the previous slide will be like it’s shown in the screenshot below!
Callback functions
Let’s look another example (house building) where we want to continue with our code only when the previous process is done.
3
Promises
Promises
Objects that represent the eventual completion or failure of asynchronous operation, and its resulting value. Generally used for easier handling of asynchronous operations such as file operations, API calls, DB calls, IO calls...
Promise can be created like it is shown in the code below.
The constructor accepts a function called executor which accepts two parameters resolve and reject. resolve() function should be executed when expected result is returned, and reject() in case when unexpected error occured.
Promises
Code from slide 15 (callback hell), now can be rewritten with promises:
Promises
In example on the right side, two promises are created. Each promise resolves after specific time expires.
By use of then() we can handle the promise and get the result of execution. With catch() we can handle errors, and finally() is always executed.
Promises also can be handled in the way that we want to wait until all promises resolve.
Try this example to get better understanding.
Promises
Three states of promises:
Promise - static methods
Most common static methods:
Use promises whenever you are using as async code.
Find more about Promises here.
4
ES6+ examples
ES6+
During this last presentation some of the most common ES6 features were shown, such as:
In this presentation we’ve explained Promises and Class syntax that is also part of ES6+.
You can find out what else was introduced in ES6 here.
On the next slides few important ES6+ features will be explained.
ES6+ (Arrow functions)
An arrow function expression has a shorter syntax compared to function expressions and does not have its own this, arguments, super, or new.target.
Two factors influenced the introduction of arrow functions:
Read more about arrow functions
ES6+ (Destructuring)
Destructuring allows as to extract data from arrays or objects into distinct variables in an easy way.
// Array destructuring
const fruits = [‘apple’, ‘orange’, ‘banana’];
let [ fruit1, fruit2, fruit3 ] = fruits;
// Object destructuring
const person = {
name: ‘John’,
age: 35
};
let { name, age } = person;
ES6+ (Spread operator)
Spread operator allows us to explode an array into its individual items.
Let’s say we want to push items from one array to another, se example on the right.
// Array destructuring
let fruits = [‘apple’, ‘orange’, ‘banana’];
let moreFruits = [‘mango’, ‘kiwi’];
// If we just push moreFruits to fruits�// then we’ll push whole array into first array.�// We don’t want this.�first.push(moreFruits);
console.log(first); �// [‘apple’, ‘orange’, ‘banana’, [‘mango’, ‘kiwi’]]
// Instead we just want to push array items inside
first.push(...moreFruits);
console.log(first); �// [‘apple’, ‘orange’, ‘banana’, ‘mango’, ‘kiwi’]
ES6+ (Template literals)
Template literals are enclosed by backtick `` character which allows us to contain placeholders ${} and write strings in multiple rows.
let a = 5;
let b = 2;
console.log(`The sum is ${a + b}`); �
ES6+ (Modules)
Coding Javascript is all about managing our scope and our variables. How you organize the code is going to have a big impact on how well you can maintain your code.
ES6+ (Modules - Exports)
To access objects in a module, a module has to mark them for export. There are two kinds of exports:
ES6+ (Modules - Imports)
Imports are used to import objects from another module. Whoever is using the module should understand how the module was built. Some modules export multiple objects, which we have to import via the named import. For the modules that export single/default object, we need to use default import:
5
Hands-on
Hands-on
6
Homework
Homework