2 Pillars: Closures & Prototypal Inheritance Flashcards

1
Q

What are some reasons that functions might be considered first class citizens in JavaScript?

A
  1. Functions can return functions
  2. Functions can be stored in data structures
  3. Functions can be passed as arguments to other functions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a closure, and how/why would you use one?

A

a closure is an inner function that has access to the outer (enclosing) function’s variables—scope chain. The closure has three scope chains: it has access to its own scope (variables defined between its curly brackets), it has access to the outer function’s variables, and it has access to the global variables.

Why would you use one?

Hiding of information that is unnecessary to be seen by the outside world or manipulated
Main benefits of closures of data encapsulation b/c some data not directly exposed

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

Explain how prototypal inheritance works*

A

This is an extremely common JavaScript interview question. All JavaScript objects have a prototype property, that is a reference to another object. When a property is accessed on an object and if the property is not found on that object, the JavaScript engine looks at the object’s prototype, and the prototype’s prototype and so on, until it finds the property defined on one of the prototypes or until it reaches the end of the prototype chain. This behavior simulates classical inheritance, but it is really more of delegation than inheritance.

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

Why is the prototype inheritance useful?

A
  • You can have objects pointing to the same place in memory, it’s more efficient
  • can easily make copies
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the 4 Pillars of OOP?

A
  1. Encapsulation - wrap code into boxes that are related to one another so they can interact w/ each other. More maintainable & reusable.
  2. Abstraction - hiding the complexity from the user, creating simpler interfaces
  3. Inheritance - ‘favorite’ party of OOP
  4. Polymorphism - many forms. Ability to call the same method are diff objects & each object responds in diff ways. Customize methods. Don’t need to copy & paste, reuse functionality.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly