Things I've learnt from Wesbos.com course
var
variables can be updated or mutated, which means you can re-delare a var
again.
var
variables are function scope. Which means their only available inside the function that they were created. Or, global scope if they've been created outside a function.
const
and let
are block scope which means they are confined inside any block, even if
statements.
Convention
- use const by default
- only use let if rebinding is needed
- (var shouldnโt be used in ES6)
https://mathiasbynens.be/notes/es6-const
- more consise
- implicit returns (means you can write one liners)
- it doesn't re-bind the value of this
You can return an object by wrapping the block in parethasese.
When not to use this If you don't want to bind this to the function.
const beemer = new Car('bmw', 'blue');
const subie = new Car('Subaru', 'white');
Car.prototype.summarize = function() {
return `This car is a ${this.make} in the colour ${this.colour}`;
};
You also don't get access to the arguments object when used inside of an arrow function https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/arguments
New functions
.startsWith()
.endsWith()
.includes()
.repeat()
Array.from()
create an array from an iterabbleArray.of(1, 2)
create an array from valuesArray.some()
check at least one condition is trueArray.every()
check to see if every condition is true
const postsPromise = fetch('http://wesbos.com/wp-json/wp/v2/posts');
postsPromise
.then(data => data.json())
.then(data => { console.log(data) })
.catch((err) => {
console.error(err);
})
When you put a method on the original constructor it will be inherited by all the other instances. There are two ways to make a class in ES6:
- Class decleration
class Dog {}
- Class expression
const Dog = class {}
Static methods are methods that only live on the parent class itself and cannot be accessed by instances e.g Array.from()
.
Its a Set
that contains objects (only) that are held weakly which means that if they are deleted from the code you are running, they are also delted from the Set
.
- Can only ever contain objects
- Its not iterable You can't enumerate or loop over it
- There isn't a
.clear()
// collected by the garbage collection when deleted in the code
Works similar to Sets
however, they have a key: value
pair.
You can loop over them in 2 ways:
foreach
which will give you a key and valuefor of
which returns an array that you can destructor (below)
const dogs = new Map();
dogs.set('Snickers', 3);
dogs.set('Sunny', 2);
dogs.set('Hugo', 10);
dogs.forEach((val, key) => console.log(val, key));
for (const [key, val] of dogs) {
console.log(key, val);
}