Object Oriented Programing Flashcards

1
Q

What is an object made of?

A

Properties define what makes up an object. Note that similar objects share the same properties, but may have different values for those properties. For example, all cars have wheels, but not all cars have the same number of wheels.

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

In OOP, what are methods?

A

They are objecct properties that are functions.

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

In OOP, what does the “this.” keyword do?

A

It makes the variable inextricably associated with the object in which it is declared.

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

In OOP, what is a constructor?

A

A function that creates an object. It defines properties and behaviors that will belong to the new object.

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

What are the rules for constructors?

A

They are defined with a capitalized name to distinguish them from other types of functions.

They use the keyword this to set properties of the object they will create. Inside it, this refers to the new object it will create.

They define properties and behaviors instead of returning a value as other functions might.

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

Visualise the creation of a new object.

Comment on the “this” keyword.

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

What happens if you don’t use the “new” keyword while creating an object?

A

Without the new operator, this inside the constructor would not point to the newly created object, giving unexpected results.

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

What is this code?

A

A constructor with parameters that can be set by the user.

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

What’s an object called in relation to its constructor?

A

It’s called an instance of its constructor.

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

In JavaScript, what does the operator “instanceof” do?

A

“instanceof” allows you to compare an object to a constructor, returning true or false based on whether or not that object was created with the constructor.

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

In JavaScript, what happens when you use “instanceof” for an object that wasn’t instantiated using a given constructor?

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

In OOP, what is an “own property”?

A

The properties that are defined directly upon instantiation of an object. Which means that each object instantiated from a given contructor will have their own copies of these properties.

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

In JavaScript, what is a prototype?

A

A way to give a property to all instances of a class/constructor.

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

What is the difference between “own property” and prototype?

A

“Own properties” are defined directly on the object instance itself while prototype properties are defined on the prototype.

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

Visualise code that would separate an object’s “own properties” from its prototype properties by adding them onto separate arrays.

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

In JavaScript what is the “constructor” property, how could it be used and what should be noted about it’s utility?

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

What is an efficient way to add multiple prototypes to a class/constructor?

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

What’s one crucial side effect of manually setting the prototype to a new object?

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

How can you fix the set back of manually setting the prototype to a new object?

A
20
Q

How are class/constructor protorypes inherited?

A
21
Q

Which JavaScript objects have a prototype?

A

All objects in JavaScript (with a few exceptions) have a prototype. Also, an object’s prototype itself is an object.

22
Q

Which JavaScript objects is “Object” object a supertype of?

What’s the inverse of supertype?

A

It is a supertype for all objects in JavaScript.

Which means that all objects in JavaScript can access its methods.

Subtype.

23
Q

Visualise an example of a prototype chain from the very top to an given object.

A
24
Q

What does DRY stand for in programming?

Why is this principle followed?

A

Don’t Repeat Yourself.

Because any change requires fixing code in multiple places. This usually means more work for programmers and more room for errors.

25
Q

How could you change the code below to conform to the DRY principle?

A

By creating a supertype (or parent) called Animal:

26
Q

In JavaScript, what is the best way to create an instance of a supertype (parent object)?

Which step is this in the process of setting inheritance?

A

First step.

27
Q

What are the two first steps in making a new object inherit behaviour from a supertype (parent object)?

A
  1. Making an instance of the supertype
  2. Set the prototype of the subtype (child object) to be an instance of the supertype.
28
Q

What is the second step in setting inheritance?

A

Setting the prototype of the subtype (or child)—in this case, Bird—to be an instance of Animal.

29
Q

What happens, constructor-wise, when when an object inherits its prototype from the another object?

How can this be solved?

A

It also inherits the supertype’s constructor property.

30
Q

Visualise a JavaScript complete inheritance and construction setting process?

A
31
Q

In JavaScript, how do you add extra (i.e. that are not in the supertype’s prototype) methods to a subtype?

A
32
Q

In JavaScript, how can supertype-inherited methods be overriden in the subtype’s object prototype?

A
33
Q

List the steps of how JavaScript looks for methos in the prototype chain of an object?

A
34
Q

In JavaScript, what is used to add the same method to unrelated objects with common behaviour?

A

Mixin

35
Q

Explain the notion of “closure” in JavaScript.

A
36
Q

What does IIFE stand for in JavaScript?

Explain it.

A
37
Q

What is a very common use of IIFE?

A

An immediately invoked function expression (IIFE) is often used to group related functionality into a single object or module.

38
Q

Return the provided string with the first letter of each word capitalized. Make sure the rest of the word is in lower case.

For the purpose of this exercise, you should also capitalize connecting words like the and of.

A
39
Q

You are given two arrays and an index.

Copy each element of the first array into the second array, in order.

Begin inserting elements at index n of the second array.

Return the resulting array. The input arrays should remain the same after the function runs.

A
40
Q

Visualize JavaScript code that removes all falsy values from an array.

A
41
Q

Visualize JavaScript code that returns the lowest index at which a value (second argument) should be inserted into an array (first argument) once it has been sorted. The returned value should be a number.

For example, getIndexToIns([1,2,3,4], 1.5) should return 1 because it is greater than 1 (index 0), but less than 2 (index 1).

Likewise, getIndexToIns([20,3,5], 19) should return 2 because once the array has been sorted it will look like [3,5,20] and 19 is less than 20 (index 2) and greater than 5 (index 1).

A
42
Q

Visualise JavaScript code that returns true if the string in the first element of the array contains all of the letters of the string in the second element of the array.

For example, [“hello”, “Hello”], should return true because all of the letters in the second string are present in the first, ignoring case.

The arguments [“hello”, “hey”] should return false because the string hello does not contain a y.

Lastly, [“Alien”, “line”], should return true because all of the letters in line are present in Alien.

A
43
Q

Write a JavaScript function that splits an array (first argument) into groups the length of size (second argument) and returns them as a two-dimensional array.

A
44
Q

Distinguish between key, property and value.

A
45
Q

What does the .every() method do?

A

It tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.