Classes arrive to JavaScript in ES6, here are a few notable features:
Easy inheritance with 'extends'
class Person {
constructor(name) { //constructors!
this.name = name;
}
introduction(){
console.log("I'm " + this.name);
}
}
class Worker extends Person { //inheritance
constructor(name, profession) {
super(name); //call the parent class with super
this.profession = profession;
}
introduction(){
super.introduction();
console.log("I am a " + this.profession);
}
}
var dev = new Worker("Dan", "Web Developer");
dev.introduction(); // logs: I'm Dan | I am a Web Developer
Managing scope gets easier in ES6, here's how arrow functions help:
No need to store references on local variables for access within deeper function blocks
More streamlined than past APIs where a bind parameter can be passed as an argument. (Array forEach, map, etc.)
// Without arrow function
function TickTock() {
var self = this; // locally assign var for 'this'
self.seconds = 0;
setInterval(function() {
// this has a different scope, so we use 'self'
console.log(++self.seconds + " seconds have passed");
}, 1000);
}
// With arrow function
function TickTock() {
this.seconds = 0;
setInterval(() => {
// now the outside scope of 'this' is retained
console.log(++this.seconds + " seconds have passed");
}, 1000);
}
JavaScript gets a legit module loader...finally:
/* ------ lib.js ------ */
export function square(x) {
return x * x;
}
/* ------ main.js ------ */
import { square } from 'lib';
console.log(square(11)); // 121
Block scoped variables help keep code in its place:
var num = 0; // num is globally scoped
for (let i = 0; i < 10; i++) { // i is block scoped
num += i;
console.log('value of i in block: ' + i);
}
console.log(typeof i); // undefined
Templating has moved beyond vanilla strings and custom user functions:
var transaction = {
customer: "Jane",
product: "sandwich",
price: 8,
sale: true
};
function printTransaction(obj){
return `Hello ${obj.customer},
do you want to buy this ${obj.product}
for ${obj.price * (obj.sale ? 0.75 : 1)} dollars?`;
}
printTransaction(transaction);
React to, and manipulate, object accessors with custom logic:
var obj = {};
var count = 0;
var myNameIs = new Proxy(obj, {
get (receiver, key) {
if (count > 3) return "You are out of chances!"
else {
count++;
return key.toLowerCase() == "rumpelstiltskin" ?
"How did you guess!?!" :
"That's not my name."
}
}
});
var g1 = myNameIs.bob; // "That's not my name."
var g2 = myNameIs.tracy; // "That's not my name."
var g3 = myNameIs.rumpelstiltskin; // "How did you guess!?!"