-
- A new reserved keyword provided by ES2015
- The class keyword creates a constant - can not be redeclared
- The
class
keyword is an abstraction of constructor functions and prototypes because Javascript does not have OOP class
does not hoist - declare classes at the top of the file- Still use the
new
keyword to create objects
- 1st create a constructor function
- use
new
to create new objects
- use the class keyword instead of creating a function
- inside, use a special method constructor which is run when new is used
- use the new keyword to create objects
-
Passing along methods and properties from one class to another
- Set the prototype property of a constructor to be an object created from another prototype property
- Reset the constructor property on a constructor function
use the
extends
keyword -
- Notice the duplication in the Student constructor function!
- Use
call
orapply
in a constructor function -apply
is handy when there are many arguments
- Super can only be used if a method by the same name is implemented in the parent class
-
What you should know about ES6 Maps - Hackernoon
- Also called "hash maps" in other languages
- Until ES2015 - objects were replacements for maps
- Similar to objects, except the keys can be ANY data type!
- Created using the
new
keyword
we can access everything with
.entries()
and destructuring!- Finding the size is easy - no more loops or
Object.keys()
- The keys can be any data type!
- You can accidentally overwrite keys on the Object.prototype in an object you make - maps do not have that issue
- Iterating over keys and values in a map is quite easy as well
- If you need to look up keys dynamically (they are not hard coded strings)
- 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
-
- Similar to a map, but all keys MUST be objects
- Values in a WeakMap can be cleared from memory if there is no reference to them
- More performant than maps, but can not be iterated over
-
- 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, ES2015 finally brings them to JavaScript
-
- Similar to a set, but all values MUST be objects
- Values in a WeakSet can be cleared from memory if there is no reference to them
- More performant than sets, but can not be iterated over
-
- 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
- Libraries have implemented Promises for a while, ES2015 is a little late to the game
- 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).
- If all of the passed-in promises fulfill, Promise.all is fulfilled with an array of the values from the passed-in promises, in the same order as the promises passed in.
- You may have seen something like this when $.when in jQuery or Q
- The promises don't resolve sequentially, but Promise.all waits for them
-
Iterators and Generators MDN doc
- A special kind of function which can pause execution and resume at any time
- Created using a
*
- When invoked, a generator object is returned to us with the keys of
value
anddone
. - Value is what is returned from the paused function using the
yield
keyword - Done is a boolean which returns true when the function has completed
We can place multiple yield keywords inside of a generator function to pause multiple times!
We can use generators to pause asynchronous code!
-
Create copies of objects without the same reference!
If we have objects inside of the object we are copying - those still have a reference!
-
Convert other data types into arrays
-
- Invoked on arrays
- Accepts a callback with value, index and array (just like forEach, map, filter, etc.)
- Returns the value found or undefined if not found
Similar to find, but returns an index or -1 if the value is not found
-
returns a boolean if a value is in a string - easier than using indexOf
-
A handy way for handling NaN being a typeof number