On Github pertrai1 / es6-workshop
Presented by Rob Simpson / @pertrai1
Wednesday - History, let, const, block functions
Thursday -
Friday -
Oh hey, these are some notes. They'll be hidden in your presentation, but you can see them if you open the speaker notes window (hit 's' on your keyboard).ES6 Compatability Table
http://bit.ly/es6tableGives a complete overview of where the API's are currently in browser implementation
There is a REPL that will allow you to practice with the code that we are discussing. It is important that you start to practice writing the new syntax.
Google homepage in 1998
The evolution of JavaScript
Day 1
cont and let to replace var
Your code will execute at runtime just as it appears at development time
Does that happen now?
Using var, where you put your code has less to do with the way it executes
Quick example:
What happens with this code at compile time?
function topLevel() { foo = "ES"; innerLevel(); function innerLevel() { bar = 6; } } topLevel();
Trap variables inside functions only - no block scope
var topic = "ES5"; function workshop() { var topic = "ES6"; console.log(topic); } workshop(); console.log(topic);
ES6 changes to add block scope
Some functions get hoisted also
// function declaration function foo() { // code } // function expression var bar = function() { // code }
Some functions get hoisted also
var b = undefined; // function declaration function foo() { // code } // function expression // variable assignment does not happen until // the code gets here bar = function() { // code }
Basic form
var topic = "Agilex"; if (true) { let workshop = "ES6"; } console.log( topic + workshop );
AH! There is block scoping
Using var for same variable will return no errors
Using let you will receive a SyntaxError
var and let act similar except let has a scope of the block in which defined as well as any sub-blocks were they are not defined.
function varTest() { var x= 31; if (true) { var x = 71; // same variable console.log(x); // 71 } console.log(x); // 71 }
function letTest() { let x = 31; if (true) { let x = 71; // different variable console.log(x); // 71 } console.log(x); // 31 }
Can't reassign a const
const a = "hi"; a = "hello"; // a is read-only
Reference: ECMAScript
How people would try to get temporal scope
function tryMe() { if (true) { let topic = "ES6"; } }
Also known as Temporal Dead Zone
No conditional needed. This can be used instead
function tryMe() { { let topic = "ES6"; } }
Providing a declarative system of parts for defining object-based abstractions.
Object-based Abstractions
Reference: ECMAScript Object Literals
Take for example:
var employee = { // shorthand for property:property position, // no function keyword needed code() { return 'I am coding'; } }
We can now use Object.setPrototypeOf using enhanced object literal to link multiple objects via prototype chain.
var employee = { code() { return 'I am coding'; } }; var employer = { pay() { return 'I am paying you'; } }; Object.setPrototypeOf(employer, employee); employer.code();
Question and Answer time