w5d5 Flashcards

1
Q

What is ‘this’ in each context:

/*sayHi includes a this reference to this in its definition */
cat.sayHi();
var crazySayHi = cat.sayHi();
crazySayHi ()
A

In the first sayHi() call is the cat object itself. In the second call to crazySayHi(), this will refer to the crazySayHi var itself.

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

What does ‘this’ refer to in each of the three ways to call functions in JS?

A

In function style, this === window

In method style, this === what it was called on

In constructor style, this refers to the new (blank) object that is being created.

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

What would happen when you call a constructor function like so:

Cat(“sennacy”, 7);

A

Since you are calling it on the global scope, you will be setting properties of the global object; the ‘new’ keyword is required to make a new object.

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

What is __proto__ effectively the Ruby equivalent of?

A

The inheritance chain.

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

How can you delete a property?

A

delete objectName.propertyName;

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

What is the command to get the class of an object?

A

typeof obj;

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

What is the syntax to monkey patch a class?

A

Array.prototype.func = function () {}

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

Why do [] == [] and [] === [] both return false?

A

B/C the equality operator does not know how to compare the contents of arrays; it is merely checking to see if they are the same object (i.e. identical address in memory)

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

What is a callback?

A

A callback is a function that is passed to another function and intended to be called at a later time. The most common use of callbacks is when a result will not be immediately available, typically because it relies on user input.

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

What is the callback in the following example:
// wait 2000 milliseconds and then print great movie link:
window.setTimeout(function () {
console.log(“http://en.wikipedia.org/wiki/Out_of_Time_(2003_film)”);
}, 2000);

A

The console.log function; When given within the anonymous function, it tells the “setTimeout” function what to do when it reaches the time when it must call the callback.

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

What is an asynchronous function?

A

A function that does not wait for work to be completed; it runs in the background. An example is a click handler: it waits until it detects a click, then runs the appropriate callback function.

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

What is the JS library required for server-side input

A

readline

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

Do asynchronous functions return meaningful values?

A

Asynchronous functions do not return meaningful values: we give them a callback so that the result of the async operation can be communicated back to us.

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

How must we call a method to set ‘this’?

A

Call it method-style

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

What is the advantage of the code below in relation to the closure’s treatment of ‘this’?

A

Because sumCalculator is a normal local variable (as opposed to the special this variable), it can be captured and used by the closure in the usual way. Later, when the closure is called function style, it won’t matter, because instead of using this (which would have been reset to window ), we instead use sumCalculator , which holds the old reference to the SumCalculator object that addNumbers was called on.

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

How can we pack up a class to export it?

A

module.exports = ClassName

Note that this is equivalent to, and is really shorthand for, module.exports.ClassName

17
Q

If you need to break a line into multiple lines, where must the line break occur?

A

Immediately after an operator.