Introduction to OOP Flashcards
How does OOP structure a problem compared to procedural programming?
OOP is a programming paradigm that organizes a problem in terms of objects and how they interact with each other.
Any OO problem will have many possible approaches. In choosing an approach for OOP, there will always be tradeoffs. There is never one absolutely correct solution.
Procedural programming solves a problem in a series of steps that are performed one after the other.
What are some advantages and disadvantages of OOP?
Advantages:
- OOP models objects by using real-world nouns to represent objects, making it easier to think about a problem at a higher-level of abstraction.
- OOP reduces the amount of dependencies throughout a large, complex program.
- OOP code is easier to maintain since it is more flexible and easy to understand.
Disadvantages:
- Often longer programs
- Possibly less efficient code, requiring more memory or computing power
Describe the concept of encapsulation.
Encapsulation is the idea of bundling state (data) and behavior (operations) to form a single entity (e.g. an object).
In its broader sense, encapsulation also refers to restricting the access to an object’s state and behaviors.
Objects reveal a public interface when interacting with other objects but keeps their implementation details hidden so their data cannot be easily manipulated. An object should only expose the methods and properties that other objects need to use.
Unfortunately, JS doesn’t support access restrictions. There are ways to achieve a degree of access restriction, but they’re not perfect.
What is the compact method syntax?
Emission of the colon : and the ‘function’ keyword. A parenthesis is used to denote a method.
Example ‘drive’ method uses compact method syntax:
let raceCar = { make: 'BMW', fuelLevel: 0.5, engineOn: false,
startEngine: function() {
this.engineOn = true;
},
drive() {
this.fuelLevel -= 0.1;
},
}
What are collaborators in OOP?
An object or primitive value that is used to store state within another object.
Collaborators represent the connections between various actors in a program therefore playing an important role in modeling complicated problems.
They are at the core of OOP since they allow the problem to be chopped up and modularized into smaller pieces.
Example, ‘cat’ object is a collaborator of ‘pete’ object. We can use pete.pet to call makeNoise().
let cat = { name: 'Fluffy',
makeNoise() {
console.log(‘Meow! Meow!’);
},
};
let pete = {
name: ‘Pete’,
pet: cat,
printName() {
console.log(My name is ${this.name}!
);
console.log(My pet's name is ${this.pet.name}
);
},
};
What are object factories aka factory functions?
Functions that create and return objects of a particular type (e.g. race cars).
It is a way to automate object creation and reduce code duplication.
The methods remain the same across the objects, while the property values can be customized by providing them as arguments.
What are ways to iterate over an object’s properties? What are the differences between the methods?
- for/in loop iterates over an object’s properties, including properties from its prototype chain. Use ‘hasOwnProperty’ to skip the prototype properties.
- Object.keys returns an object’s “own” property keys, excluding properties from its prototype chain.
Both methods deal with enumerable properties, which are properties you can iterate over.
What is a prototype chain? How does it work?
A prototype chain are all inherited prototypes of an object.
For example, if an object ‘b’ inherited from object ‘a’ and an object ‘c’ inherits from object ‘b’, then both object ‘a’ and ‘b’ are part of the prototype chain of object ‘c’.
let a = { foo: 1 }; let b = { bar: 2 }; let c = { baz: 3 };
Object.setPrototypeOf(c, b);
Object.setPrototypeOf(b, a);
console. log(c.bar); // => 2
console. log(c.foo); // => 1
What is the _ _ proto _ _ property?
The dunder proto property is a deprecated, non-hidden version of the [[Prototype]] property.
As a rule, you should only use __proto__ if you need to support very old browsers or old versions of Node.
What are ‘bare’ objects?
‘Bare’ objects are objects without Object.prototype at the end of its prototype chain. You can create a ‘bare’ object by using ‘Object.create(null)’.
‘Bare’ objects won’t have access to usual object methods like .hasOwnProperty.
To check if an object is a ‘bare’ object:
if (Object.getPrototypeOf(obj) && obj.isPrototypeOf(car)) { // obj has a non-null prototype AND // obj is in the prototype chain of car }
Describe the concept prototypal inheritance.
The idea that JavaScript objects can inherit properties and behaviors from other objects.
Prototype is the object that another object inherits properties and methods from (i.e. parent object).
How does the Object.create method work?
Object.create takes a prototype object as an argument and returns a new object with inherited properties.
The new object will not have any properties or methods of its own. The new object’s [[Prototype]] property is assigned to the original prototype object.
Example:
let a = { foo: 1, bar: 2} let b = Object.create(a) console.log(b.foo) // => 1 console.log(b) // => { } (empty object)
What are the functions of Object.getPrototypeOf and Object.setPrototypeOf?
Object.getPrototypeOf() returns the prototype object of the passed in object.
Object.setPrototypeOf() takes in two arguments. First is the prototype object to inherit and second is the object you would like to set the prototype property.
Why are prototype properties considered references?
Prototype properties are references, so changing the ORIGINAL prototype object will also mutate the inherited object’s prototype properties.
What are higher-order functions?
Higher-order functions are functions that either:
- Return another function
- Takes another function as an argument
Examples of functions that takes a function as an argument: array methods - map, filter, forEach, reduce