|
|
7 years ago | |
|---|---|---|
| .. | ||
| README.md | 7 years ago | |
| castle.md | 8 years ago | |
| landscaper.md | 8 years ago | |
README.md
ES6 - The Future Is Now! (Sort of)
Lesson Objectives
- What is ES6
- strict mode
- understanding MDN
- polyfills and shims
- babel/transpiling
- IFFE
- Block level scoping with let
- const
- arrow functions
- Classes
- for...of
- default value setting
- Array.isArray()
- argument object
- spread and rest operators
- Trailing commas
- Template Literals
- Object literal extensions
- Destructuring
- swap (desconstucturing method)
- Merge objects
- Array functions
- Asynchronous programming
What is ES6
ECMAScript is the standard version of JavaScript. ECMA stands for European Computer Manufacturers Association. ES5 became standard in 2009. ES6 (or ES2015), became standard in 2015. But just because it is made standard, it doesn't mean that all browsers/environments are ready/compliant.
Strict Mode
Adding 'use strict' to the top of your code, (see Babel screenshot) will let you opt in to some newer features of JS! More info
understanding MDN
-
Lets you know which methods/syntaxes are standard, experimental, depreciated, when things were added and more including shims/polyfills.
-
Quickly see if a method is standard[no notation], experimental[flask], depreciated (removed from standards, may be removed)[thumbs down], or obsolete (likely removed altogether in newer environments)[trash can]
-
See more information about when the method was defined implemented:
Polyfills/Shims
Some people call them polyfills, some call them shims; either way: It is code that is a fallback that provides the functionality when the expected functionality is not available (because old browser, etc. ). Many entries in MDN have polyfills. This is also a great way to look at how the ECMAScript standard code is written (a lot of examples are directly from the official ECMAScript docs)
An example Polyfill:
babel/transpiling
Node.js supports most/nearly all new things. Chrome, Firefox, Edge, and Safari have been keeping up well. But new features are being added and tested. To be sure that code will run across many platforms (never forget the people who still use IE8), many people have come to rely on transpilers/compilers. Transpilers/Compilers take 'dialects' of standard code (incuding ES6, coffeescript, typescript and more) and convert the code for you. A popular one for ES6 is Babel, they have both repl (Read, Evaluate, Print, Loop) and an npm module/gem.
You can also run babel from within the command line, but most often it's used a build tool like webpack. First install:
npm init
npm install babel-cli
npm install babel-preset-es2015
Now in your package.json add this into "scripts":
"scripts": {
"build": "babel --presets es2015 test.js -o compiled.js"
},
Now create a test.js file:
touch test.js
In it, write some ES6:
class Car {
run(){
}
}
Now run npm run build and take a look at compiled.js:
"use strict";
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Car = function () {
function Car() {
_classCallCheck(this, Car);
}
_createClass(Car, [{
key: "run",
value: function run() {}
}]);
return Car;
}();
That's a spicy meatball!
If you want, you can adjust package.json to re-compile every time you make a change:
"scripts": {
"build": "babel --watch --presets es2015 test.js -o compiled.js"
},
IIFE
Normally, variable declarations, are "hoisted" up to the top of whatever function they are declared in (or global if no function exists). This can lead to some weird moments:
var a = 2;
if(true){
var a = 4;
}
console.log(a); //in many programming languages, this would be 2
and a famous interview question:
for(var i=0; i<5; i++){
setTimeout(function(){
console.log(i) //you'd expect 0,1,2,3,4
},500 * i);
}
IIFE - Immediately Invoked Function Expression - often used to create a block of scope. This is not an ES6 thing, but rather an example of an old way to get block scoping.
var a = 2;
(function(){
var a = 4;
console.log ('Inside the IFFE, the value of a is', a ); //"Inside the IFFE, the value of a is 4"
})();
console.log ( 'Outside the IFFE, the value of a is', a ); //"Outside the IFFE, the value of a is 2"
And now the loop with setTimeout:
for(var i=0; i<5; i++){
(function(i){
setTimeout(function(){
console.log(i)
},500 * i);
})(i)
}
Block Scoping with let
A new way to get block level scoping is with the keyword let, and the use of {} as in any {}, not just tied to a function! Note: let is NOT meant to completely replace var!
let a = 2
{
let a = 4
console.log( 'the value of a inside the `{}` is', a); //"block level scope"
}
console.log ('the value of a outside of the `{}` is', a) //"global scope"
And now the loop with setTimeout:
for(let i=0; i<5; i++){
setTimeout(function(){
console.log(i) //you'd expect 0,1,2,3,4
},500 * i);
}
console.log(i); //notice i is not defined outside of the loop
Const
Another new way to declare a variable. This will block the value from being reassigned.
const pi = 3.14;
pi = "pie" //TypeError: Assignment to constant variable.
const whatsForLunch = [ "Nuts", "Coffee" ]
whatsForLunch.push( "Yogurt" ) //you can still call object methods that alter the variable
console.log( whatsForLunch)
whatsForLunch.shift(); //you can still call object methods that alter the variable
console.log( whatsForLunch )
whatsForLunch = ["salad"]; //TypeError: Assignment to constant variable.
Arrow Functions
A new way to write a function expression (arrow functions are always anonymous functions), with fun shortcuts:
Shorthands
Basic:
var basicES5 = function(){
return 'oh hai';
}
//take out the keyword 'function' and add in '=>'
var basicES6 = () => {
return 'oh hai'
}
If the body of the function is just one expression that gets returned, we can remove the {} and ES6 knows to return whatever comes after the =>
var basicES6 = () => 'oh hai'
Multiple arguments:
//ES5 - Multiple arguments
var sumES5 = function (a, b){
return a+b;
}
console.log('ES5 sum:',sumES5(5,5));
//ES6 - Multiple arguments
var sumES6 = ( a, b ) => a + b;
console.log('ES6 sum:', sumES6(6,6));
If we only have one argument to the function, we can leave out the ()
//ES5 - one argument
var squareES5 = function (c){
return c*c;
}
console.log('ES5 square:', squareES5(5));
//ES6 - one argument
var squareES6 = c => c*c
console.log('ES6 square:',squareES6(6));
Binding
with callbacks, this can get redefined:
function Person(name){
this.name = name;
this.logName = function(){
setTimeout(function(){
console.log(this.name);
},500)
}
}
var me = new Person('Matt');
me.logName();
arrow functions fix this:
function Person(name){
this.name = name;
this.logName = function(){
setTimeout(()=>{
console.log(this.name);
},500)
}
}
var me = new Person('Matt');
me.logName();
Support for Classes
class Cat {
static getTypes(){
return ['large', 'small'];
}
constructor(name) {
this.name = name;
}
makesNoises() {
return this.name + ' meows';
}
purrs(){
return this.name + ' purrs';
}
}
class Lion extends Cat {
makesNoises() {
return this.name + ' roars!';
}
eatHuman(){
return 'yum';
}
}
var myCat = new Cat("Kitty");
console.log(myCat.makesNoises());
console.log(myCat.purrs());
var myLion = new Lion("Simba");
console.log(myLion.makesNoises());
console.log(myLion.purrs());
console.log(myLion.eatHuman());
console.log(Cat.getTypes());
This makes inheritance much easier than how it used to be:
var Person = function(){
this.greet = function(){
console.log('oh hai!');
}
}
var SuperHero = function(){
Person.call(this);
this.fly = function(){
console.log('up up and away!');
}
}
SuperHero.prototype = Object.create(Person.prototype);
SuperHero.prototype.constructor = SuperHero;
var superman = new SuperHero()
superman.fly();
superman.greet();
for...of
Until now, the best way to loop through an array was either:
var array = [1,4,6];
for(var i =0; i < array.length; i++){
console.log(array[i]);
}
Or:
var array = [1,4,6];
array.forEach(function(element){
console.log(element);
});
But now we can do:
var array = [1,4,6];
for(let element of array){
console.log(element);
}
Default values
When creating a function and you wanted a default value, you previously had to write something like this:
var multBy2 = function(value){
var defaultValue = (value)?value:0;
return defaultValue * 2
}
console.log(multBy2());
Now you can do this in ES6
var multBy2 = function(value = 0){
return value * 2
}
console.log(multBy2());
Array.isArray()
If you have tried to confirm if something is an array with ES5, you may have found it frustrating:
var arr = [1,2,3];
console.log(typeof arr); //'object'
You could do
arr instanceof Array; // true
But now there is a new method!
Array.isArray(arr); //true
Argument Object
This isn't an ES6 thing, but should help expand your knowledge of arguments in JS.
The arguments object is an array-like object (but not fully an array, so you cannot always call array methods on it). It allows for a lot of flexibility in the number of arguments a function can take:
function sum (){
var sum = 0;
for(var i = 0; i < arguments.length; i++ ){
sum += arguments[i];
}
return sum;
}
console.log( sum(1,2,3,4) ); //10
Spread and Rest Operators
Spread operator:
Spread Operator will take an array, and convert each element into its own value. It "spreads" the elements out into values
Let's look at Math.max() which takes any number of arguments and finds the maximum value
//Standard syntax
console.log(Math.max(1,5,-6)); // 5
- But what if you have an array of values?
- Converting each to a variable and then adding them as arguments is a pain, until now:
var z = [1,5,-2,8,-9,17,-22];
console.log(Math.max(...z)); // 17
Rest operator:
The rest operator gathers many values into an array. It's basically the opposite of spread.
function returnOnlyNums(...arrayParam){
var nums = arrayParam.filter(function(currentElement){
return typeof currentElement === 'number'
})
return nums;
}
console.log( returnOnlyNums(44, false, 'pizza', 45, {season: "winter"}, [1,2,3,4,5,], 2, 9) ); // [ 44, 45, 2, 9 ]
Trailing commas
You can add trailing commas to make copy/paste easier later on:
var arr = [
1,
2,
3, //the comma here will not cause issues
];
console.log(arr);
If you have multiple commas, it will create holes in the array:
var arr = [
1,
2,
3,,,,
];
console.log(arr);
console.log(arr.length); //logs 6
Trailing commas are okay in objects too:
var object = {
foo: "bar",
baz: "qwerty",
age: 42,
};
console.log(object);
Template Literals (String Interpolation)
Template literals allow you to insert variables into strings with out having to use +
The old way:
var name = "Matt"
console.log ('Hello '+ name);
Now we can do this:
var name = "Matt"
console.log (`Hello ${name}.`);
You can even evaluate JavaScript within the Template Literal:
var name = "Matt";
function yell(value){
return value.toUpperCase();
}
console.log (`Hello ${yell(name)}!`);
Multiple lines used to suck:
var adjective = 'awesome'
var template = 'I wrote a haiku\n'+
'This is so very ' + adjective + '\n'+
'Now I am finished';
console.log(template);
Now they're easy!
var adjective = 'awesome';
var template = `I wrote a haiku
This is so very ${adjective}
Now I am finished`;
console.log(template);
Object literal upgrades
Instead of:
var a = 8;
var b = 9;
var c = 10;
var es5obj = {
a:a,
b:b,
c:c,
d : function (){
console.log(this.a, this.b, this.c);
}
}
es5obj.d();
Now do:
var a = 8;
var b = 9;
var c = 10;
var es6obj = {
a,
b,
c,
d(){
console.log(this.a, this.b, this.c);
}
}
es6obj.d();
Destructuring
Destructuring pulls variables out of their "structure" (array or object)
Array Destructuring
You can assign values an array to variables easily:
var a, b;
[a,b] = [10,12]
console.log(a);
console.log(b);
You can also declare in the same line as assignment:
var [a,b] = [10,12]
console.log(a);
console.log(b);
You can ignore certain values:
var a, b;
[a,,b] = [1,2,3,4];
console.log(a);
console.log(b);
You can also use a rest operator to gather variables into an array after a certain point:
var a, b, rest;
[a,b,...rest] = [10, 20, 30, 40, 50];
console.log(a);
console.log(b);
console.log(rest);
You can set defaults as well:
var a, b;
[a=5, b=7] = [1];
console.log(a); // 1
console.log(b); // 7
Object Destructuring
Pull variables out of an object
var {p, q} = {p: 42, q: true};
console.log(p); // 42
console.log(q); // true
You can also change the names of the variables:
var o = {p: 42, q: true};
var {p: foo, q: bar} = o;
console.log(foo); // 42
console.log(bar); // true
You can also do default values:
var {a = 10, b = 5} = {a: 3};
console.log(a); // 3
console.log(b); // 5
You can pull values from a function argument that's an object:
var user = {
id: 42,
displayName: 'jdoe',
fullName: {
firstName: 'John',
lastName: 'Doe'
}
};
function userId({id}) {
return id;
}
console.log('userId: ' + userId(user)); // "userId: 42"
Use this for default options arguments:
function drawES2015Chart({size = 'big', cords = {x: 0, y: 0}, radius = 25}) {
console.log(size, cords, radius);
}
drawES2015Chart({
cords: {x: 18, y: 30},
radius: 30
});
If you have a property name which is not a valid variable name, you can reassign it:
const foo = { 'fizz-buzz': true }
const { 'fizz-buzz': fizzBuzz } = foo
console.log(fizzBuzz);
Lastly, you can compute a variable name:
let key = 'z';
let {[key]: foo} = {z: 'bar'};
console.log(foo); // "bar"
Swap values: File under Destructuring Assignment
If you wanted to swap the value of x and y with es5, you had to do
var x = true;
var y = false;
var temp;
temp = x;
x = y;
y = temp;
console.log(x,y);
The new way will return x and y to original values in this case
var x = true;
var y = false;
[x,y] = [y,x];
console.log(x,y);
Merge objects
You can use Object.assign() to merge properties of two or more objects:
var a = {
foo:'bar'
}
var b = {
awesome:true
}
Object.assign(a, b);
console.log(a);
This is extremely useful when you need to create a brand new object that is a clone of another one with some minor changes (happens all the time with Redux):
var bob = {
name:'Bob',
age: 42
}
var sally = Object.assign({}, bob, {name:'Sally'})
console.log(sally);
Array functions
There are a bunch of cool new array helper functions:
var nums = [1,3,5,7,8];
var largeNums = nums.filter(function(currentNum){
return currentNum > 5
});
console.log(largeNums);
var sum = nums.reduce(function(accumulatedValue, currentNum){
return accumulatedValue + currentNum;
});
console.log(sum);
var doubledValues = nums.map(function(currentNum){
return currentNum * 2;
})
console.log(doubledValues);
var found = nums.find(function(currentNum) {
return currentNum > 5;
});
console.log(found);
var found = nums.findIndex(function(currentNum) {
return currentNum > 5;
});
console.log(found);
var doesFiveExist = nums.includes(5);
console.log(doesFiveExist);
var doubleArray = nums.concat(nums);
console.log(doubleArray);
var section = nums.slice(2,4)
console.log(section);
Asynchronous programming
Asynchronous programming used to be filled with nested callbacks and promises:
fetch('http://www.omdbapi.com/?apikey=53aa2cd6&s=Star').then(function(response){
response.json().then(function(searchData){
fetch('http://www.omdbapi.com/?apikey=53aa2cd6&t='+searchData.Search[0].Title).then(function(response){
//NOTE: this function is a closure, so we could still reference searchData
response.json().then(function(titleData){
console.log(titleData);
});
});
})
})
But now using Aync/Await, we can make our code much easier to read:
var getData = async function(){
var searchResponse = await fetch('http://www.omdbapi.com/?apikey=53aa2cd6&s=Star');
var searchData = await searchResponse.json();
var titleResponse = await fetch('http://www.omdbapi.com/?apikey=53aa2cd6&t=' + searchData.Search[0].Title);
var titleData = await titleResponse.json();
console.log(titleData);
}
getData();



