Section 17: ES2015 Part II Flashcards

1
Q

class keyword

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

class example

A
class Student {
   constructor(firstName, lastName){
       this.firstName = firstName;
       this.lastName = lastName;
    }
}

var elie = new Student(‘Elie’, ‘Schoppik’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Where and how do you place prototype methods with the ‘class’ keyword?

A
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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a static method and how do you declare one?

A

*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;
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Inheritance

A
  • Passing along methods and properties from one class to another.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

‘extends’ keyword

A

*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 {

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

super keyword

A
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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

maps or “hash maps”

A

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));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why would you use a map?

A
  1. Finding the size is easy - no more loops or Object.keys()
  2. They keys can be any data type!
  3. No worring that you might overwrite keys on the Object.prototype.
  4. Iterating over keys and values in a map is quite easy as well.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

When to use a map?

A
  1. If you need to look up keys dynamically
  2. If you need keys that are not strings
  3. If you are frequently adding and removing key/value pairs.
  4. Are key-value pairs frequently added or removed?
  5. If you are operating on multiple keys at a time.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

sets

A
  • 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

how do you create a set?

A

var s = new Set;

var s2 = new Set([3,1,4,1,2,1,5]); // {3,1,4,2,5}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are some methods on set?

A

.has()
.delete()
.size

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Define a Promise

A
  • 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Promise.all

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Promise.all example

A
function getMovie(title) {
   return $.getJSON(`https://omdbapi.com?t=${title}&apikey=thewebd`);
}
var titanicPromise = getMovie('titanic');
var shrekPromise = getMovie('shrek');
var braveheartPromise = getMovie('braveheart');

Promise.all([titanicPromise, shrekPromise, braveheartPromise]).then(function(movies){
return movies.forEach(function(value){
console.log(value.Year);
});
});

17
Q

Generators

A
  • A special kind of function which can pause execution and resume at any time
  • Created using ‘*’
  • When invoked, a generator object is returned to us with the keys of value and done.
  • 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.
18
Q

Object.assign

A

Create copies of objects without the same reference

var o = {name: "Ellie"};
var o2 = Object.assign({}, o);

o2. name = “Tim”;
o. name; // “Elie”

**Not a deep clone.

19
Q

Array.from

A
convert other data types into arrays
//Example 1
var divs = document.getElementsByTagName("div");
var converted = Array.from(divs);

//Example 2

var firstSet = new Set([1,2,3,4,3,2,1]); // {1,2,3,4}
var arrayFromSet = Array.from(firstSet); //[1,2,3,4]
20
Q

find

A
  • Invoked on arrays
  • Accepts a callback with value, index and array
  • Returns the value found or undefined if not found