Object Oriented Programming Flashcards
Why OOP?
Clear + Understanable
Easy to extend
Easy to maintain
Memory Efficient
DRY
What Closures and Prototypes influenced from something?
Closures are similar to FP(Functional Programming)
Prototypes are similar to OOP(Object Oriented Programming)
What are the object property variables called In OOP called?
States
What are functions called in OOP?
Methods
What are factory functions?
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
What is a store?
Store in OOP is where we store the values in Object.
What is the stores?
Which only has stores and methods/manipulations are defined outside.
Example of a store.
Example of inheritance without class
Inheritance without using Object.create?
We can use the constructor function. with the new keywords and this keyword.
Needs title casing.
What is a Constructor Function?
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”);
Why do we need new in constructor function?
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.
What are the constructors in class in ES6?
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.
What are the difference between methods and functions?
Methods are defined inside a class.
Difference between Object.create() vs class?
Many call Object.create() is a pure prototypal inheritance.
Newer Es6 codebase people use class for OOP characteristics.
this - 4 ways?
- new binding
- implicit binding
- explicit binding
- 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()