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.