Functions: Closures, HOF and Prototypes Flashcards

1
Q

Why is Function First class citizens?

A
  1. We can assign it to the variable
  2. Pass it as a variable
  3. Return it as a variable

Which is not possible in many languages.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is function constructor?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What happens in Function execution context?

A

Function Execution context has this, arguments and all looks at the variables and store them in memory heap.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How is function an object in javascript?

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are callable objects in javascript?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does function object in javascript contain?

A

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..

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is Higher ordered function?

A

Higher order function:
A higher-order function takes a function as an argument, returns a function, or both.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What and when do we use is Higher order function?

A

A Higher Order FuncBon (HOF) is a function that either takes a function as an argument or returns another function.

Example of the HOF.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Example of HOC?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the 2 pillars of Javascript?

A
  1. Closure
  2. Prototypal Inheritance
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a closure?

A

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();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a difference between closure & HOC?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Can you write an example of closure?

A

// 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you see closures in console?

A

By using console.dir to the parent function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is scope chaining?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do we make closure memory efficient?

A

In the example in picture, big array is only created once and used multiple time in with closures.

17
Q

What is encapsulation?

A

Encapsulation means the restriction of direct access to some of an object’s components. It hides as much as possible of an object’s internal parts and only exposes the necessary parts to run. Why use encapsulation?

• Security - Controlled access
• Hide ImplementaBon and Expose Behaviours
• Loose Coupling - Modify the implementation at any time

18
Q

How to encapsulate with closures?

A

In this example, there is no direct access to the data people array in the closure. using set, get and remove we are manipulation and fetching the data.

19
Q

How do you solve this?

A

We can solve it by using let so by using IIFE (Immediately Invoked Function Expression)

20
Q

What is the difference between IIFE and Closure?

A

IIFE helps avoid global variable pollution when multiple functions are accessing a global variable
Closure functions are helpful when working with a local variable.

21
Q

What is Prototype?

A

Array and Functional are Objects. But they get these properties because of Prototypal Inheritance.

Example:
arr = []

arr.__proto__
In console.log, you will find methods like push, pop and slice.

And if you do
arr.__proto__.__proto__
You will find methods like toString object; because of prototype chaining, we can use the “toString” method directly in the array.

In the case of a function, it points to native function which points to an object.

22
Q

In Javascript do we have class?

A

Javascript doesn’t have class, it has the prototypal inheritance. In ES6, we use class which compiles down to these prototypes.

23
Q

What is the different between bind and prototypal inheritance?

A

With bind, you can bind a few properties, but can’t inherit the entire object.

24
Q

What is prototype chain?

A

Each object has a private property which holds a link to another object called its prototype. That prototype object has a prototype of its own, and so on until an object is reached with null as its prototype.

So basically it’s a linked list to the parent object prototype.

25
Q

How do we write prototypal inheritance with an example?

A

let father = { surname: “Ch”, language : “English” }
let mother = { language : “Telugu” }
let son = { }
mother.__proto__ = father
son.__proto__ = mother

son.language // will be telugu.

mother.isPrototypeOf(son) // true
father.isPrototypeOf(son) // true
son.isPrototypeOf(father) // false
son.isPrototypeOf(mother) // false

26
Q

How to check the properties of the function?

A

Using “hasOwnProperty(propName)”

For list of all the properties we can loop the object.

27
Q

What is the difference between lexical scopes vs prototype scopes?

A

Lexical scopes are passed from parent to child.
Prototypes are inherited to parent to child.

28
Q

Is is a good approach to directly use __proto__ to assign and inherit?

A

No, it’s not and it’s a terrible approach for performance

29
Q

What is the difference between prototype and __proto__?

A

__proto__ points to the “prototype” up the chain.
__proto__ lives in the same object lives in “prototype”.

const arr = []

arr.hasOwnPropety(“map”) // return false.
arr.__proto__.hasOwnPropety(“map”) // return true.

So map should live only in one location in the memory

30
Q

How to create a prototype a safe way?

A

Which Object.Create

let human = { mortal: true }
let socrates = Object.create(human)
socrates.age = 45
console.log(human.isPrototypeOf(socrates))

31
Q

Imposter Syndrome?

A

Teaching while we learn is important thing.