JavaScript Flashcards
What is the difference between class and prototypal inheritance?
Class inheritance: Classes are like a blueprint, a description of the object to be created. Classes inherit from classes and create subclass relationships
Prototypal inheritance: A prototype is a working object instance. Objects inherit directly from other objects.
What is a Closure?
A closure gives you access to an outer function’s scope from an inner function.
Use cases for closures?
- Data Privacy / Emulating Private Methods: Closures are commonly used to give objects data privacy. By utilizing closures, we can make it so we can’t get data except through the object’s methods. That way, malicious code can not interact with the data.
- Event Handlers and Callbacks: JavaScript is heavy on asynchronous operations, and closures are often used in event handlers and callbacks.
- Factory functions are functions that return other functions. Closures are especially useful here because they allow each returned function to have its own private state.
What is a pure function?
A pure function is a which which:
- Given the same input, always returns the same output.
- Produces no side effects, which means that it can’t alter any external state
Motivations for pure functions?
- Avoiding shared state (can cause bugs difficult to debug)
What is hoisting?
Hoisting allows you to use functions and variables before they’re declared.
What is functional programming?
Functional programming is the process of building software by composing pure functions, avoiding shared state, and side effects.
- Declarative rather than imperative
Benefits/motivations for functional programming
More concise, more predictable, and easier to test than OOP code.
What is a promise?
A promise is an object that may produce a single value some time in the future, either a resolved value, or a reason it’s not resolved.
Promises are how javascript handles asynchronous code/operations.
3 states of a promise?
Pending, fullfilled, rejected
null vs undefined
undefined means value of the variable is not defined.
null means empty/non-existent
If you want to use an arbitrary object as value of this, how will you do that?
There are at least three different ways to doing this by using bind, call and apply.
What does bind do?
bind allows you to borrow a method and set the value of this without calling the function. It simply returns an exact copy of the function with new value of this
What is this in JavaScript?
At the time of execution of every function, JavaScript engine sets a property to the function called this which refer to the current execution context. this is always refer to an object and depends on how function is called
What is currying in JavaScript?
currying is when a function — instead of taking all arguments at one time — takes the first one and returns a new function, which takes the second one and returns a new function, which takes the third one, etc. until all arguments are completed.
What is currying in JavaScript?
currying is when a function — instead of taking all arguments at one time — takes the first one and returns a new function, which takes the second one and returns a new function, which takes the third one, etc. until all arguments are completed.
When should someone use currying?
- It divides your function into multiple smaller functions that can handle one responsibility, which makes errors less likely
- It is used in functional programming to create a higher-order function
What is the prototype chain?
The prototype chain in JavaScript is a series of linked prototype objects that forms a chain-like structure. This chain is used in the lookup process when the program needs to access a property or a method of an object: the property is looked up not only on the object itself but also on its prototype, the prototype’s prototype, and so on, until the property is found or the chain ends. At the end of this chain is Object.prototype, whose prototype is null, signifying the end of the chain.
What is a callback?
A callback is a function that gets passed as an argument to another function and is executed after some operation has been completed.
What was the motivation for creating Promises?
Promises were introduced in ES6 to solve the problems of callback hell (nested callbacks).
What is a promise?
A Promise represents a value which may not be available yet, but will be resolved at some point in the future or rejected entirely.
What is the .then() method?
then() is a method that takes a callback for when the Promise is resolved
What is the .catch() method?
catch() is a method that takes a callback for when the Promise is rejected.
What is the async keyword used for?
An async function is a function that knows how to expect the possibility of the await keyword being used within it