Essential JS book Flashcards
What is Duff’s device?
A way to better handle loops when there are a lot of items to iterate over. It breaks the total into sets of 8 and then processes 8 at a time using a do/while and a switch statement with fall-through.
What is a best practice for avoiding global variables?
Namespacing.
MyNamespace = {};
MyNamespace.EventUtil = { … }
This is worth the tradeoff in lookups and code-writing because of maintainability
What is the difference between a comma operator and a comma separator?
Comma separators simply delimit members in a list (as in an array).
Comma operators partition expressions within statements.
What’s a good way to handle calculations with decimals (e.g. with currency)?
Convert to integers, do math, convert back to decimals.
Why is the isNaN( ) method problematic and what’s a more reliable solution?
It reports true for things that are not numbers, e.g. “foo” or undefined, because of type coercion.
return ( x !== x ) will solve this problem. “foo” !== “foo”; // false NaN !== NaN; // true
Semicolons are only inserted when?
Before a } token, after one or more newlines, or at the end of the program. However, if the new line’s initial token could be interpreted as a continuation of the statement. a + b \n var c; is fine. a + b \n +c; not fine.
Finally, semicolons are inserted after certain keywords, the ‘restricted productions’
What are JavaScript ‘restricted productions’ and what do they do?
return, throw, break, continue, ++, - -
You cannot inert a line break between one of these and the rest of the statement; a semicolon will be inserted.
return \n someObject will return nothing.
Never omit a semicolon before a statement beginning with what characters?
+, -, / , (, or [
What is the one exception to JavaScript’s lack of block scoping?
Exceptions. try / catch binds a caught exception to a variable that is scoped just to the catch block. That is, the x in catch( x ) { } will only have scope inside.
What is a higher order function?
A function that takes a function as an argument or returns a function as a result.
What is a variadic function?
one which accepts a variable number of arguments.
What is arity?
The number of arguments or operands a function or operation takes.
If you need to modify a function’s arguments object, what should you do?
First, never modify the arguments object directly.
Do it indirectly like so:
var args = Array.slice.call( arguments ) or Array.slice.call( arguments, 1 );
What is a the “receiver” of a method call?
It’s the thing that gets bound to “this” and it’s determined by the call itself.
obj1. someFunc( ); // obj1 is receiver
obj2. newFunc( ); // ( newFunc: obj1.someFunc;) obj2 is receiver
obj2. forEach( ob1.someFunc ); obj2 is the receiver
How can you use bind( ) to have setTimeout( ) use a particular object for ‘this’?
var timer = setTimeout( someObj.method.bind( someObj ), 1000 );
How can you use bind( ) to curry a function?
bind takes a thisArg (which can be null) plus n number of arguments which prepend any arguments provided to the bound function.
MassTowns.map( getLocation.bind( null, “U.S.”, “Massachusetts” )); // might create [ “Acton, Massachusetts, U.S.”, “Brockton, Mass… etc ]
SomeObj.prototype.func = function( arr ) {
return arr.map( function( item ) {
return item + this.prop;
} } // What is the receiver of this?
arr, because map function binds it’s callback receiver to its array.
Review the arguments accepted by array methods map, filter, some, forEach, and every…
map ( callbackFunction, [ thisArg ] )
callback takes item, index, and array.
thisArg is the this-binding for the callback.
The scope of ‘this’ is always determined by what?
Its nearest enclosing function.
How could you remove an item, “myItem”, from an array based on the name of the item?
var i = myArray.indexOf( "myItem" ); if ( i >= 0 ) { myArray.splice( i, 1 ); }
How can we make SubClass( ) a sub class of SuperClass( ) ?
SubClass.prototype = Object.create( SuperClass.prototype ); // Better than SubClass.prototype = new SuperClass() because we can’t pass instance arguments and other problems can occur.
Sub class inherits from Super and var subby = new Sub( ); Object.getPrototypeOf( subby ) === Super.prototype; True or False?
False, because getPrototypeOf() returns the internal [[prototype]] of subby, which is Sub.prototype, not Super.prototype.
Sub class inherits from Super and var subby = new Sub( ); Super.prototype.isPrototypeOf( subby ); True or False?
True, because isPrototypeOf( ) tells you if the receiver (Super.prototype) is in the prototype chain of subby, which it is.
Sub class inherits from Super through prototype chain. Object.getPrototypeOf( Sub.prototype ) === ??? What will make this true?
Super.prototype.
How do you check if the value of something is undefined?
if ( typeof === “undefined”)…
Note quotes.
How do you create a merge/extend function?
Pass two objects, target and source, to an extend function. Each has n number of properties, possibly overlapping. Do a for…in loop of source and for each key that isn’t undefined, add it to the target: target[key] = val.
Is the HTML canvas API stateful or stateless?
It’s stateful, because behavior of it’s functions depend on the changing state of the canvas element. For example, cnvs.fillText( “hello” ) will be different depending on font or color settings of the canvas.
How is Array.isArray different from arr instanceof Array and different from Object.prototype.toString( arr ); // “[object Array]” ?
isArray is ES5 only and is most reliable. instanceof will not work for instances of array that come from other frames (since they won’t inherit from the same Array.prototype). Last version works well if you can’t guarantee ES5.
urls.forEach( function( url, i ) { // do something asynchronously. Callback includes... result[i] = text; if ( result.length === urls.length ) ...
This creates a problem, because result.length could equal urls.length with only one part finished. If result[3] = ‘something’, result.length will be at least 4.
Why should you never call asynchronous callbacks synchronously?
disrupts the expected sequence of operations;
can lead to stack overflows
purpose of asynch is to maintain strict separation of turns of the event loop