Ironhack Day 16

ES6

0. ES6 compatibility table: https://kangax.github.io/compat-table/es6/

1.Using classes:

class Product {
constructor (name, price) {
this.name = name;
this.price = price;
}

nameAndPrice () {
console.log(
”The product’s name is: ” + this.name,
”The product’s price is: ” + this.price
);
}
}

const banana = new Product(”Banana”, 2);
banana.nameAndPrice();

2. Inheritance and overwriting methods (see the second nameAndPrice below):

class Product {
constructor (name, price) {
this.name = name;
this.price = price;
}

nameAndPrice () {
console.log(
”The product’s name is: ” + this.name,
”The product’s price is: ” + this.price
);
}
}

class Electronic extends Product {
constructor (name, price, brand) {
super(name, price);
this.brand = brand;
}

nameAndPrice () {
console.log(
”The product’s name is: ” + this.name,
”The product’s price is: ” + this.price,
”and the brand is ” + this.brand
);
}
}

var banana = new Product(”Banana”, 2);
banana.nameAndPrice();

var mac = new Electronic(”Mac”, 200, ”Apple”);
mac.nameAndPrice();

3. If statements and for loops don’t have own scope:

for (var i = 1; i <= 30; i++){
console.log(”Iteration number: ” + i);
}

console.log(”After the loop”, i);
//After the loop 31

4a. Block scoping with let (incorrectly):

for (let i = 1; i <= 30; i++){
console.log(”Iteration number: ” + i);
}

console.log(”After the loop”, i);
//for After the loop: ReferenceError i is not defined

4b. Block scoping with let (correctly):

let name = ”Ironhacker”;

if (true){
let name = ”Ted”;
console.log(”Name inside of if statement: ” + name);
}

console.log(”Name outside of if statement: ” + name);

// Name inside of if statement: Ted
// Name outside of if statement: Ironhacker

5. Although you can’t reassign ’con’, you can still modify its properties:

const TAXES = {
normal: 0.21
}

console.log(TAXES)

TAXES.normal = 0.23

console.log(TAXES)

// { normal: 0.21 }

// { normal: 0.23 }

6. Arrow functions are syntactic sugar over the typical function declaration, with an added bonus: a new function scope is not created:
class Counter {
constructor(){
this.count = 1;
}

countUp(){
setInterval(() => {
console.log(this.count);
this.count++;
}, 1000);
}
}

var myCounter = new Counter();
myCounter.countUp();

Vastaa

Sähköpostiosoitettasi ei julkaista. Pakolliset kentät on merkitty *