Functions: Closures, HOF and Prototypes Flashcards
Why is Function First class citizens?
- We can assign it to the variable
- Pass it as a variable
- Return it as a variable
Which is not possible in many languages.
What is function constructor?
const four = new Function(“return 4”)
four() // return 4;
const num = new Function(“a”, “b”, “return a+b”);
num(4,5) // return 9;
The last parameter is always the code.
What happens in Function execution context?
Function Execution context has this, arguments and all looks at the variables and store them in memory heap.
How is function an object in javascript?
Functions are stored in javascript as special objects callable objects.
There is an internal property, [[Call]], that determines what will be executed when the object is called.
Function constructor
const num = new Function(“a”, “b”, “return a+b”)
num.foo = 3;
What are callable objects in javascript?
Functions are stored in javascript as special objects callable objects.
There is an internal property, [[Call]], that determines what will be executed when the object is called.
Plain objects don’t have this internal property, so they aren’t callable, and can’t be made to be callable.
The only callables in JS are functions (which are also objects), classes (which are actually functions, and therefore objects as well), and Proxy objects that wrap callables.
What does function object in javascript contain?
In JavaScript, functions are first-class objects, because they can have properties and methods just like any other object.
Function has:
Name: Name of the function.
paraments: The name of an argument to be passed to the function.
statements: The statements comprising the body of the function.
Properties: call(), apply(), bind(), etc..
What is Higher ordered function?
Higher order function:
A higher-order function takes a function as an argument, returns a function, or both.
What and when do we use is Higher order function?
A Higher Order FuncBon (HOF) is a function that either takes a function as an argument or returns another function.
Example of the HOF.
Example of HOC?
What are the 2 pillars of Javascript?
- Closure
- Prototypal Inheritance
What is a closure?
A closure combines a function bundled together (enclosed) with references to its surrounding state (the lexical environment).
function init() {
var name = ‘Mozilla’; // name is a local variable created by init
function displayName() {
// displayName() is the inner function, a closure
console.log(name); // use variable declared in the parent function
}
displayName();
}
init();
What is a difference between closure & HOC?
Higher order function:
A higher-order function takes a function as an argument, returns a function, or both.
Closure:
A closure combines a function bundled together (enclosed) with references to its surrounding state (the lexical environment).
So a function can be closure and HOF.
PFA image of HOF and Closure combined.
Can you write an example of closure?
// Explanation of closure
/* 1 / function foo()
/ 2 / {
/ 3 / var b = 1;
/ 4 / function inner(){
/ 5 / return b;
/ 6 / }
/ 7 / return inner;
/ 8 / }
/ 9 / var get_func_inner = foo();
/ 10 / console.log(get_func_inner());
/ 11 / console.log(get_func_inner());
/ 12 */ console.log(get_func_inner());
Explanation: Note line number 9 to line number 12.
At line number 9, we are done with the execution of function foo() and the entire body of function inner() is returned and stored in var get_func_inner due to the line 7 return inner.
We can access the variable b which is defined in function foo() through function inner() as the later preserves the scope chain of enclosing function at the time of execution of enclosing function i.e. the inner function knows the value of b through it’s scope chain.
How do you see closures in console?
By using console.dir
to the parent function.
What is scope chaining?
Scope chaining is how the variables are passed from the parent function to the
As we can see, the variables within the closure in the scope section.