JavaScript Flashcards
Can you name two programming paradigms important for JavaScript developers?
- Imperative programming, with procedural and object-oriented programming paradigms supported
- Functional programming, an example of which being closures
What is the difference between classical inheritance and prototypal inheritance?
- Inheriting from a class means an object will have the attributes and methods that the super class defines
- Prototype inheritance means that an object will have a reference to attributes and methods that the prototype has, and uses these as a fallback
What does map() do? (2)
- An array method, the map() method creates a new array with the results of calling a function for every array element.
- The map() method calls the provided function once for each element in an array, in order.
What does reduce() do? (3)
- An array method, the reduce() method reduces the array to a single value.
- The reduce() method executes a provided function for each value of the array (from left-to-right).
- The return value of the function is stored in an accumulator (result/total).
What does filter() do? (2)
- An array method, the filter() method creates an array filled with all array elements that pass a test (provided as a function).
- The filter() method executes a provided test for each value of the array (from left-to-right).
What is a stack? How would you implement a stack in JS? (3)
- A stack is a FILO data structure, can be an array in JS
- Use the pop() method to remove a value from the top of the stack
- Use the push() method to add value(s) to the top of the stack
What is a queue? How would you implement a queue in JS? (3)
- A queue is a FIFO data structure, can be an array in JS
- Use the shift() method to remove a value from the front of the queue
- Use the unshift method to add value(s) to the front of the stack
What does call() do in JS? (2)
- It can be used to invoke (call) a method with an owner object as an argument (parameter).
- What this means is if a method includes
this
, then this will refer to the argument object - It takes arguments separately, i.e if there are n values then there are n arguments
What does apply() do in JS? (2)
- It can be used to invoke (call) a method with an owner object as an argument (parameter).
- What this means is if a method includes
this
, then this will refer to the argument object - It takes arguments as an array, i.e if there are n values then the argument is an array of n elements
What is lexical scope?
Scope created within a function, defined with var
What is block scope?
The keywords let
and const
define block scoped variables in pairs of curly brackets, otherwise JS doesn’t support block scope
What is scope chaining?
If you think of nested scoping as a tree structure, where a function b() nested in a() is a child node to a(), a scope chain is the path from a node to the root node
What sits at the top of every scope chain?
A global scope object, which is window
in the browser and global
in node
What is scope shadowing?
In a nested function, declaring a lexically scoped variable with the same name as a lexically scoped variable in the parent function is valid, and the nested function only has access to it’s locally scoped variable (normally it would have access to the parents variable too)
What is a closure?
If an inner function accesses a value in an outer function, it is said to close over that value, hence is a closure. This inner function can then be returned from the outer function, and still access the outer variable.
In other words, closure is when a function is able to remember and access its lexical scope even when that function is executing outside its lexical scope.