ES6 Flashcards
What three new methods does ES6 introduce for identifying whether a given string contains a substring?
includes(), startsWith() and endsWith() // These won’t work with regular expressions
What are tagged template literals?
Functions that receive pieces of a template literal as arguments which you can manipulate and return as html.
How can you easily clone an array using rest parameters?
let […clonedArray] = myArray;
For function doSomething () { … } how can you get the name “doSomething” from within or without the function?
doSomething.name
How can you make sure a function was called with the ‘new’ keyword?
if (typeof new.target !== ‘undefined’) { // the function was called using new. This is better than checking if this instanceof Person, for example.
Make an arrow function into an immediately invoked function: (value) => {
doSomething(value)
return value;
}
Wrap in parens: ((value) => {…})(‘Something’)
What’s a tail call?
When a function is called as the last statement in another function the compiler has trouble optimizing.
How can you optimize for tail calls when they are necessary?
Don’t require access to variables in the current stack frame (the function can’t be a closure); the function making the tail call has no more work todo after the tail call returns; the result of the tail call is returned as the function value.
How does Object.is() work?
Accepts 2 arguments and returns true if they are the same type and value. Object.is(NaN, NaN); // true
How does Object.assign() work?
Object.assign(receiver, supplier); It takes two objects and adds the properties/values from supplier to receiver. It modifies receiver.
What does the super() method do? How does it work?
‘super’ is a pointer to the current object’s prototype. It’s like saying Object.getPrototypeOf(this). It must be inside a ‘concise’ method to work.
What is a concise method of an object literal?
Instead of var person = { getAge: function() { … } we can say var person = { getAge() { … } ,
Why send destructured parameters to a function? What does this mean/do?
It helps clarify what the function requires: function myFunction (name, value, { url, filename, size } = { } ) { ... } // Note: the parameters are required if you don't set a default
Add newItem onto the end of array someArray using spread operator
[ …someArray, newItem ]
How to make this work:
var a, b;
{a, b} = {a: 1, b: 2};
Move the destructing into parenthesis:
var a, b;
({a, b} = {a: 1, b: 2});
or use let/const/var
var {a, b} = {a: 1, b: 2};