10. @boyney123
The let statement declares a block scope local variable, optionally initializing it to a value.
if (x > y) {
let gamma = 12.7 + y;
i = gamma * x;
}
let allows you to declare variables that are limited in scope to the block,
statement, or expression on which it is used.This is unlike the var keyword,
which defines a variable globally, or locally to an entire function regardless of
block scope.
ES6 : LET
13. @boyney123
The const declaration creates a read-only reference to a value. It does not mean the value it
holds is immutable, just that the variable identifier cannot be reassigned.
const API_URL = “http://www.some-api-url.co.uk”;
ES6 : CONST
16. @boyney123
ES6 : MODULES
COMMONJS AMD
var $ = require('jquery');
exports.myExample = function () {};
define(['jquery'] , function ($) {
return function () {};
});
Common on browserCommon on server
The goal for ECMAScript 6 modules was to create a format that
both users of CommonJS and of AMD are happy with.
20. @boyney123
Syntactical sugar over JavaScript's existing prototype-based inheritance.The class
syntax is not introducing a new object-oriented inheritance model to JavaScript.
JavaScript classes provide a much simpler and clearer syntax to create objects and
deal with inheritance.
class Person {
constructor(firstname, surname) {
this.firstname = firstname;
this.surname = surname;
}
}
To declare a class, you use the class keyword with the name of the class ("Person" here).
ES6 : CLASSES
24. @boyney123
In JavaScript, parameters of functions default to undefined. However, in some
situations it might be useful to set a different default value.This is where default
parameters can help.
function multiply(a, b) {
b = typeof b !== 'undefined' ? b : 1;
return a*b;
}
multiply(5); // 5
ES6 : DEFAULT PARAMETERS
function multiply(a, b = 1) {
return a*b;
}
multiply(5); // 5
ES5 example
ES6 example
26. @boyney123
An arrow function expression (also known as fat arrow function) has a shorter
syntax compared to function expressions and lexically binds the this value (does
not bind its own this, arguments, super, or new.target).
Arrow functions are always anonymous.
var a = [
"Hydrogen",
"Helium",
"Lithium",
"Beryllium"
];
var a2 = a.map(function(s){ return s.length });
var a3 = a.map( s => s.length );
ES6 : ARROW FUNCTIONS
Until arrow functions, every new function defined its own this value
29. @boyney123
The Promise object is used for deferred and asynchronous computations.A
Promise represents an operation that hasn't completed yet, but is expected in the
future.
ES6 : PROMISES
Promise has three states:
pending: initial state, not fulfilled or rejected.
fulfilled: meaning that the operation completed successfully.
rejected: meaning that the operation failed.
33. @boyney123
Template strings are string literals allowing embedded expressions.You can use
multi-line strings and string interpolation features with them.
`Hello World!`
`Hello brown bag!
I cant wait for a few drinks and dance on thursday!`
let person = ‘David’;
`${person} is drunk. Send him home!`
ES6 : TEMPLATE STRINGS
var person = ‘David’;
person + ‘ is drunk. Send him home!’ ES5 example
ES6 example
37. @boyney123
async keyword ensures that the function will return a Promise object.
Within an async function any time you return a value it will actually return a
promise that is resolved. If you want to reject you need to throw an error
async function foo() {
if( Math.round(Math.random()) )
return 'Success!';
else
throw 'Failure!';
}
ES7 : ASYNC
ES7 example
function foo() {
if( Math.round(Math.random()) )
return Promise.resolve('Success!');
else
return Promise.reject('Failure!');
} ES6 example
39. @boyney123
await will stop the function call until the async has resolved or rejected.
async function loadStory() {
try {
let story = await getJSON(‘story.json');
return story;
} catch (err) {
throw ‘Failure!';
}
}
ES7 : AWAIT
40. @boyney123
let db = new PouchDB(‘mydb');
try {
//nice that we don't have to wrap in promise
let result = await db.post({});
//This reads pretty nice?
let doc = await db.get(result.id);
console.log(doc);
} catch (err) {
console.log(err);
}
ES7 : ASYNC & AWAIT EXAMPLE 2