Javascript Flashcards
What’s the difference between setInterval and setTimer?
setTimer will fire once after the duration, setInterval will continually run.. both return IDs that can be used to clear or cancel them.
setInterval will continue if an error happens.
What is bind ?
Bind is called on a function and return a new function with ‘this’ bound to the first argument.
Whats the difference between call and apply?
both will call a function immediately w/ a first arg to use as ‘this’. The difference is in remaining args, apply takes an array of args, whereas call take comma delimited args like a traditional func all.
what are javascript classes?
Introduced in ES 6 they provide a more familar way to define classes over the prototypical way.
class Bike{
getWheelCount(){ return 2}
}
what are some uses of the spread operator?
creating copies (be careful they are shallow) copies of objects and arrays.
merging objects and concatenating arrays
unpacking arrays into multiple args for a function
how do you do a deep copy of a var?
JSON.stringify into JSON.parse
what’s a pitfall of checking typeof bar === “object” ?
nulls are also an object so you must check !== null
what are benefits of ‘use strict’
will avoid accidental global vars that are made without var keyword.
what is a closure?
Closer is a function that returns a function, but sets some state in the outer function that is referenced in the inner function.
A good example is a createCounter method that might return something that can be called like counter() and output increasingly numbers for each call.
what are the three states of a promise
pending, fulfilled and rejected
how do you create a new promise?
const promise = new Promise((resolve, reject) => {
// call reject or resolve
})
what is the modern alternative to promises?
using async and await.. it allows the code to read more impertively and avoid ‘callback hell’.. functions declared w/ async can call await inside async calls to wait for the function to finish before running the next line.