w5d5 Flashcards
What is ‘this’ in each context:
/*sayHi includes a this reference to this in its definition */ cat.sayHi();
var crazySayHi = cat.sayHi(); crazySayHi ()
In the first sayHi() call is the cat object itself. In the second call to crazySayHi(), this will refer to the crazySayHi var itself.
What does ‘this’ refer to in each of the three ways to call functions in JS?
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.
What would happen when you call a constructor function like so:
Cat(“sennacy”, 7);
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.
What is __proto__ effectively the Ruby equivalent of?
The inheritance chain.
How can you delete a property?
delete objectName.propertyName;
What is the command to get the class of an object?
typeof obj;
What is the syntax to monkey patch a class?
Array.prototype.func = function () {}
Why do [] == [] and [] === [] both return false?
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)
What is a callback?
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.
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);
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.
What is an asynchronous function?
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.
What is the JS library required for server-side input
readline
Do asynchronous functions return meaningful values?
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 must we call a method to set ‘this’?
Call it method-style
What is the advantage of the code below in relation to the closure’s treatment of ‘this’?
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.