Intro to OOP Flashcards
We call this principle of combining data and the operations relevant to that data ________________
encapsulation
your own words, what is Object Oriented Programming? (2 things)
Object-Oriented Programming is a programming paradigm in which we think about a problem in terms of objects. In particular, it uses those objects to organize your program.
1 paradigm
2 Think and organize
Describe some advantages and disadvantages of OOP.
Advantages
It lets programmers think about a problem at a higher-level of abstraction, which helps them break down and solve the problem.
OOP helps programmers write programs that reduce the dependencies in a program, which makes maintenance easier.
Done right, OOP makes code flexible, easy to understand, and easy to change.
Disadvantages
OOP programs are often much larger than the equivalent procedural program.
OOP may lead to less efficient code; OO programs may require more memory, disk space, and computing power.
In JavaScript, how does encapsulation differ from encapsulation in most other OO languages?
In other languages, encapsulation concerns hiding details of an object from code that uses the object. An object should only expose the methods and properties that other objects need to use the encapsulated object. However, JavaScript does not directly provide the means to limit exposure of methods and properties. There are ways to achieve a degree of access restriction, but they’re not perfect.
What’s a collaborator object?
Give an example of a collaborator object
Objects that help provide state within another object are called collaborator objects, or more simply, collaborators.
https://ieltume.medium.com/what-the-heck-are-collaborator-objects-985e5cdd8ecf
A simple example is an object being referenced inside another object
let cat = { };
let pete = {
name: ‘Pete’,
pet: cat,
}
What’s a factory function?
A function that makes an object and returns it
Let’s say you want to create an object for 2 mammals. They have common properties: teeth, hair, nipples.
But they have different properties: trunk, puch.
We want separate factories for each but don’t want to repeat the common properties. What do we do?
Have a third factory for the common elements. They are Object.assigned .
First like this:
function createPlayer() {
return {
move: null,
};
}
Then like this:
function createHuman() {
let playerObject = createPlayer();
let humanObject = {
choose() {
let choice;
while (true) { console.log('Please choose rock, paper, or scissors:'); choice = readline.question(); if (['rock', 'paper', 'scissors'].includes(choice)) break; console.log('Sorry, invalid choice.'); } this.move = choice; }, };
return Object.assign(playerObject, humanObject);
}
What’s an interface? (not a js thing)
The state and behaviors exposed by the object for other objects to use.
What is the state of an object? What is the behaviour?
Data, property, value
Operations, Method
Are methods just collaborators? Because they’re objects?
no
another way to check if an object has a key (other than hasOwnProperty)
What’s the difference between them
aKey in anObject
in vs. hasOwnProperty. The in operator will check if the property is present, either directly in an object or in its prototype chain; whereas, the hasOwnProperty() method only checks if the property is directly present in the object.
another way to get an array of all the keys in an object (other than Object.keys)
Object.getOwnPropertyNames
Difference between Object.keys and Object.getOwnPropertyNames
Object.keys does enumerable properties only.
Object.getOwnPropertyName does all properties
What makes an enumerable property
Enumerable properties are those properties whose internal enumerable flag is set to true, which is the default for properties created via simple assignment or via a property initializer.
global is not a property of global
F