Intro to OOP Flashcards

1
Q

We call this principle of combining data and the operations relevant to that data ________________

A

encapsulation

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

your own words, what is Object Oriented Programming? (2 things)

A

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

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

Describe some advantages and disadvantages of OOP.

A

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.

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

In JavaScript, how does encapsulation differ from encapsulation in most other OO languages?

A

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.

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

What’s a collaborator object?
Give an example of a collaborator object

A

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,
}

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

What’s a factory function?

A

A function that makes an object and returns it

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

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?

A

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);
}

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

What’s an interface? (not a js thing)

A

The state and behaviors exposed by the object for other objects to use.

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

What is the state of an object? What is the behaviour?

A

Data, property, value
Operations, Method

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

Are methods just collaborators? Because they’re objects?

A

no

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

another way to check if an object has a key (other than hasOwnProperty)
What’s the difference between them

A

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.

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

another way to get an array of all the keys in an object (other than Object.keys)

A

Object.getOwnPropertyNames

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

Difference between Object.keys and Object.getOwnPropertyNames

A

Object.keys does enumerable properties only.

Object.getOwnPropertyName does all properties

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

What makes an enumerable property

A

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.

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

global is not a property of global

A

F

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

console is a property of global

A

T

17
Q

isFinite is a property of global

A

T

18
Q

All js functions are first class entities

A

T

19
Q

All js functions are anonymous

A

F

20
Q

Function declarations are anonmyous

A

F

21
Q

All arrow functions are anonymous

A

T

22
Q

function() {
xyz = 123;
}
xyz is on the global object
t or F

A

T

23
Q

What makes an array-like object?

A

All objects that have a length property.

24
Q

let str1 = new String(“abc”);
let str2 = String(“abc”);

console.log(str1 === str2); // => false
Are they the same otherwise?
How about with arrays (new Array vs Array)

A

They are different. new String creates an object

With array they’re basically the saame.

25
Q

Your program needs to change the case of all letters in a string to the opposite case. That is, Naveed Fida should be converted to nAVEED fIDA. You already have a function called convertCase that does this for a single character. It takes a single character as an argument and returns the translated result.

Given the convertCase function, which of the following code snippets can be used to convert the string contained by str? Select all answers that apply.

A
str = [1, 2, 3].map.call(str, convertCase).join(“”);

B
str = str.map(convertCase).join(“”);

C
str = Array.from(str).map(convertCase).join(“”);

D
str = str.split(“”).map(convertCase);

A

A
C

26
Q

What makes an anonymous function?

A

When we declare an function, without any identifier, it’s an anonymous function

.an anonymous function is that type of function that has no name or we can say which is without any name.

function apple() {} is a named function
let apple = function() {} is an anonymous function assigned to apple. apple.name will be apple but it’s still an anonymous function?
let apple = function orange() {} is a name function assigned to apple. apple.name = orange

arrow functions are always anonymous because you can’t assign a name ever.

27
Q

let name1 = function name2() {}

console.log(number.name1);

A

name2