Object Oriented Programming Flashcards

1
Q

Why OOP?

A

Clear + Understanable
Easy to extend
Easy to maintain
Memory Efficient
DRY

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

What Closures and Prototypes influenced from something?

A

Closures are similar to FP(Functional Programming)
Prototypes are similar to OOP(Object Oriented Programming)

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

What are the object property variables called In OOP called?

A

States

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

What are functions called in OOP?

A

Methods

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

What are factory functions?

A

We can use factory functions as a first step towards OOP.
The main drawback is using them for methods as the methods are stored in memory too

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

What is a store?

A

Store in OOP is where we store the values in Object.

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

What is the stores?

A

Which only has stores and methods/manipulations are defined outside.

Example of a store.

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

Example of inheritance without class

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

Inheritance without using Object.create?

A

We can use the constructor function. with the new keywords and this keyword.
Needs title casing.

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

What is a Constructor Function?

A

Before classes in ES6 we used to using function constructor for write classes.

function Elf(name, type, weapon) {
// not returning anything
// “constructing” a new elf
this.name = name;
this.type = type;
this.weapon = weapon;
}

const dobby = new Elf(“Dobby”, “house”, “cloth”);

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

Why do we need new in constructor function?

A

The new operator lets developers create an instance of a user-defined object type or of one of the built-in object types that have a constructor function.

Because of lexical scoping, otherwise, this element won’t be added to the variable assigned.

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

What are the constructors in class in ES6?

A

Classes are syntactic sugar for prototypal inheritance.
Constructors are precisely similar to construct functions.
Yet, they favour ES6 to bring them much closer to OOP.

The constructor method is a class method for creating and initializing an object instance of that class.
In javascript, we also bind the methods which are in array functions.

Constructor runs for each new object or instance created.

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

What are the difference between methods and functions?

A

Methods are defined inside a class.

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

Difference between Object.create() vs class?

A

Many call Object.create() is a pure prototypal inheritance.
Newer Es6 codebase people use class for OOP characteristics.

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

this - 4 ways?

A
  1. new binding
  2. implicit binding
  3. explicit binding
  4. arrow functions

// new binding
function Person(name, age) {
this.name = name;
this.age =age;
console.log(this);
}

const person1 = new Person(‘Xavier’, 55)

//implicit binding
const person = {
name: ‘Karen’,
age: 40,
hi() {
console.log(‘hi’ + this.name)
}
}

person.hi()

//explicit binding
const person3 = {
name: ‘Karen’,
age: 40,
hi: function() {
console.log(‘hi’ + this.setTimeout)
}.bind(window)
}

person3.hi()

// arrow functions
const person4 = {
name: ‘Karen’,
age: 40,
hi: function() {
var inner = () => {
console.log(‘hi ‘ + this.name)
}
return inner()
}
}

person4.hi()

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

How can we inherit using class in ES6?

A

using “extends” keyword

17
Q

How to know if the object belongs to a class?

A

By using instanceOf keyword

18
Q

So if we extends what happens?

A

In javascript, it’s basically copy one object and using that object with prototypal inheritance

19
Q

Can we create Private class fields and methods?

A

In typescript, private visibility is a design-time construct.
# is a representation of private ES2022.

ES2022: Private Class Fields + Methods

class Employee {
#name = “Test”;
constructor(name) {
this.#setName(name) // ok
}
#setName(name) { // Private method
this.#name = name;
}
}

const emp = new Employee(‘New’); // ok
emp.#setName(‘New’); // error

20
Q

4 Pillars of OOP?

A

Encapsulation
Abstraction
Inheritance
Polymorphism