JavaScript OOP Flashcards

1
Q

What is a method?

A

A method is a function which is a property of an object. There are two kinds of methods: instance methods which are built-in tasks performed by an object instance, or static methods which are tasks that are called directly on an object constructor.

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

A method definition will have a colon (:) following the name followed by an anonymous function for that method and a method call will have a set of parentheses with some or none parameters following the name of the method.

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

Describe method definition syntax (structure).

A

The method name followed by colon, then anonymous function called with parameters, then curly braces for what the function will do, including a return. This method is defined inside of an object as a property.

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

Describe method call syntax (structure).

A

The object name followed by a period (‘.’) followed by the name of the method with a set of parentheses ( ‘()’ ) filled with either some or no parameters.

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

A method is a function which is a property of an object and must be called with that 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

That objects can contain both data (as properties) and 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

Abstraction, encapsulation, inheritance, and polymorphism

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

What is “abstraction”?

A

Making complex or specific details into a more abstract object or idea that focuses on the bigger picture instead of details.

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

To give programmers a way to interact with a system in a simplified, consistent fashion (abstraction).

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

What is this in JavaScript?

A

this is an implicit parameter of all JavaScript functions.
the value of this is determined at call time.
the value of this can be recognized as “the object to the left of the dot” when the function is called (as a method).
if there is no value to the left of the dot when the function is called, then by default, this will be the global window object.
if you cannot see the function being called, then you do not know what the value of this will be.

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

It is available in a function’s code block even though it was never included in the function’s parameter list or declared with 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

The value of this is determined at the call time of a function

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

This refers to the character object when it will be called, but since the code snippet is a definition, it refers to nothing and defaults to the window object.

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

Given the above character object, what is the result of the following code snippet? Why?
character.greet();

A

‘It’s-a-me, Mario!’
This is because the greet method of the character object is being called. The greet method concatenates this.firstName, which would be the value of the firstName property of the character object since it is what is left of the dot when the function is called, with other strings.

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

Given the above character object, what is the result of the following code snippet? Why?
var hello = character.greet;
hello();

A

‘It’s-a-me, undefined!’
This is because the hello function is being called not as a method of an object, so this defaults to the window object which does not have a firstName property of character object, so this.firstName === undefined.

17
Q

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

A

You can’t because the value of this is determined at call time.

18
Q

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

A

the value of this can be recognized as “the object to the left of the dot” when the function is called (as a method).
if there is no value to the left of the dot when the function is called, then by default, this will be the global window object.
if you cannot see the function being called, then you do not know what the value of this will be.

19
Q

What kind of inheritance does the JavaScript programming language use?

A

prototype-based (or prototypal) inheritance.

20
Q

What is a prototype in JavaScript?

A

An object that contains properties and (predominantly) methods that can be used by other objects.

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

Those methods are defined on a “prototype” object and strings, arrays, and numbers simply borrow those methods when they’re needed. They inherit it from the prototype chain.

22
Q

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

A

The prototype object of the object and keeps going up through prototypes until it finds the property or method.

23
Q

What does the new operator do?

A

The new operator lets developers create an instance of a user-defined object type or of one of the built-in object types that has a constructor function.

24
Q

What property of JavaScript functions can store shared behavior for instances created with new?

A

Their prototypes

25
Q

What does the instanceof operator do?

A

The instanceof operator tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object. The return value is a boolean value.

26
Q

What is a “callback” function?

A

A function passed into another function as an argument and only invoked when the outer function is called. They’re often used to continue code execution after an asynchronous operation has completed.

27
Q

Besides adding an event listener callback function to an element or the document, what is one way to delay the execution of a JavaScript function until some point in the future?

A

The setTimeout() global function

28
Q

How can you set up a function to be called repeatedly without using a loop?

A

With the setInterval() global function which needs the clearInterval() global function to stop it.

29
Q

What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?

A

0ms or immediately.

30
Q

What do setTimeout() and setInterval() return?

A

The returned timeoutID is a positive integer value which identifies the timer created by the call to setTimeout(). This value can be passed to clearTimeout() to cancel the timeout. The returned intervalID is a numeric, non-zero value which identifies the timer created by the call to setInterval(); this value can be passed to clearInterval() to cancel the interval.
They both share the same pool of IDs and clearInterval() and clearTimeout() can technically be used interchangeably.