Object-oriented programming Flashcards

1
Q

What is a method?

A

A method is a function which is a property of an object.
In JavaScript functions themselves are objects, so, in that context, a method is actually an object reference to a function.

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

How can you tell the difference between a method definition and a method call?

A

o Method definitions will always have a function keyword and a code block
o Method calls start with an object name and has arguments that are being passed

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

Describe method definition syntax (structure).

A

propertyName: functionName( ) {
Return ‘anything’;
}

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

Describe method call syntax (structure).

A

objectName.method( )

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

How is a method different from any other function?

A

They are functions stored in an object

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

What is the defining characteristic of Object-Oriented Programming?

A

Objects can contain both:

  • Data (as properties)
  • Behavior (as methods)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the four “principles” of Object-Oriented Programming?

A

o Abstraction
o Encapsulation
o Inheritance
o Polymorphism

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

What is “abstraction”?

A

Being able to work with (possibly) complex things in simple ways (such as, turning a light on/off)

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

What does API stand for?

A

Application Programming Interface

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

What is the purpose of an API?

A

Give programmers a way to interact with a system(s) in a simplified, consistent fashion (aka abstraction) without knowing about how they all work independently.

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

What is ‘this’ in JavaScript?

A

The object that you’re working within

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

What does it mean to say that this is an “implicit parameter”?

A

The parameter is available (in a function’s code block) even though it was never included in the function’s parameter list OR declared in a var

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

When is the value of ‘this’ determined in a function; call time or definition time?

A

Call time

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

What does ‘this’ refer to in the following code snippet?

var character = {
firstName: 'Mario',
greet: function () {
var message = 'It\'s-a-me, ' + this.firstName + '!';
console.log(message);
}
};
A

Undefined (there is nothing in front of greet)

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

Given this character object:

var character = {
firstName: 'Mario',
greet: function () {
var message = 'It\'s-a-me, ' + this.firstName + '!';
console.log(message);
}
};

What is the result of the following code snippet? Why?
character.greet();

A

It’s a me, Mario! Because greet is being called on the character object

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

Given this character object:

var character = {
firstName: 'Mario',
greet: function () {
var message = 'It\'s-a-me, ' + this.firstName + '!';
console.log(message);
}
};

What is the result of the following code snippet? Why?
var hello = character.greet;
hello();

A

It’s a me, undefined!

Because ‘this’ is being called from the window object and not the character object

17
Q

How can you tell what the value of ‘this’ will be for a particular function or method DEFINITION?

A

There is no way to know in a definition because there is no value yet

18
Q

How can you tell what the value of this is for a particular function or method CALL?

A

By checking the object left of the dot

19
Q

What kind of inheritance does the JavaScript programming language use?

A

Prototype-based (prototypal) inheritance

20
Q

What is a prototype in JavaScript?

A

Object with properties and methods that can be reused

21
Q

How is it possible to call methods on strings, arrays, and numbers even though those methods don’t actually exist on strings, arrays, and numbers?

A

Via inheritance from the prototypes (prototypal inheritance)

22
Q

If an object does not have its own property or method by a given key, where does JavaScript look for it?

A

its prototype (starts from the nearest prototype and moves up until it finds what it needs)