Section 17: ES2015 Part II Flashcards
class keyword
- creates a constant that can’t be redeclared
- An abstraction of constructor functions and prototypes.
- Class keyword does not hoist, so declare them at the top.
- Still use ‘new’ keyword to create objects
class example
class Student { constructor(firstName, lastName){ this.firstName = firstName; this.lastName = lastName; } }
var elie = new Student(‘Elie’, ‘Schoppik’);
Where and how do you place prototype methods with the ‘class’ keyword?
class Student { constructor(firstName, lastName){ this.firstName = firstName; this.lastName = lastName; } sayHello() { return `Hello ${this.firstName} ${this.lastName}`; } }
** This is placing the method on the prototype and only creating it once for each of the instances. Anything you put inside of the constructor will be created again for each instance.
What is a static method and how do you declare one?
*The static keyword defines a static method for a class. Static methods aren’t called on instances of the class. Instead, they’re called on the class itself. These are often utility functions, such as functions to create or clone objects.
Example:
class Student { constructor(firstName, lastName){ this.firstName = firstName; this.lastName = lastName; } sayHello() { return `Hello ${this.firstName} ${this.lastName}`; } static isStudent (obj) { return obj.constructor === Student; } }
Inheritance
- Passing along methods and properties from one class to another.
‘extends’ keyword
*helps include one class inside of another with no additional code necessary.
class Person { constructor(firstName, lastName){ this.firstName = firstName; this.lastName = lastName; } sayHello() { return `Hello $(this.firstName) $(this.lastName)`; } }
class Student extends Person {
}
super keyword
class Person { constructor(firstName, lastName){ this.firstName = firstName; this.lastName = lastName; } sayHello() { return `Hello $(this.firstName) $(this.lastName)`; } }
class Student extends Person { constructor(firstName, lastName){ super(firstName, lastName); } }
** super can only be used if there is a method by the same name in the parent class.
maps or “hash maps”
Similar to objects, except the keys can be any data type.
Create by using new keyword.
Example: var firstMap = new Map; firstMap.set(1, 'Elie'); firstMap.set(false, 'a boolean'); firstMap.set('nice', 'a string'); firstMap.delete('nice'); //true firstMap.size // 2
*it comes with a forEach method:
myMap.forEach(v => console.log(v));
Why would you use a map?
- Finding the size is easy - no more loops or Object.keys()
- They keys can be any data type!
- No worring that you might overwrite keys on the Object.prototype.
- Iterating over keys and values in a map is quite easy as well.
When to use a map?
- If you need to look up keys dynamically
- If you need keys that are not strings
- If you are frequently adding and removing key/value pairs.
- Are key-value pairs frequently added or removed?
- If you are operating on multiple keys at a time.
sets
- All values in a set are unique
- Any type of value can exist in a set
- Created using the new keyword
- Exist in quite a few other languages
** You can implement a for … of loop with sets.
how do you create a set?
var s = new Set;
var s2 = new Set([3,1,4,1,2,1,5]); // {3,1,4,2,5}
What are some methods on set?
.has()
.delete()
.size
Define a Promise
- A one time guaranteed return of some future value.
- When that value is figured out - the promise is resolved/fulfilled or rejected.
- Friendly way to refactor callback code.
Example:
function displayAtRandomTime(){ return new Promise(function(resolve,reject){ setTimeout(function(){ if(Math.random() > .5) { resolve('Yes!'); } else { reject('no'); } }, 1000); }); }
displayAtRandomTime().then(function(value){ console.log(value); }).catch(function(error){ console.log(error); });
*not super common to use the native promise constructor. More common to use 3rd party ones, like jQuery.
Promise.all
Accepts an array of promises and resolves all of them or rejects once a single one of the promises has been first rejected. (fail fast)
*the Promises don’t resolve sequentially but promise.all waits for them to resolve.