Essential JS book Flashcards

1
Q

What is Duff’s device?

A

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.

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

What is a best practice for avoiding global variables?

A

Namespacing.
MyNamespace = {};
MyNamespace.EventUtil = { … }
This is worth the tradeoff in lookups and code-writing because of maintainability

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

What is the difference between a comma operator and a comma separator?

A

Comma separators simply delimit members in a list (as in an array).
Comma operators partition expressions within statements.

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

What’s a good way to handle calculations with decimals (e.g. with currency)?

A

Convert to integers, do math, convert back to decimals.

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

Why is the isNaN( ) method problematic and what’s a more reliable solution?

A

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

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

Semicolons are only inserted when?

A

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’

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

What are JavaScript ‘restricted productions’ and what do they do?

A

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.

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

Never omit a semicolon before a statement beginning with what characters?

A

+, -, / , (, or [

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

What is the one exception to JavaScript’s lack of block scoping?

A

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.

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

What is a higher order function?

A

A function that takes a function as an argument or returns a function as a result.

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

What is a variadic function?

A

one which accepts a variable number of arguments.

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

What is arity?

A

The number of arguments or operands a function or operation takes.

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

If you need to modify a function’s arguments object, what should you do?

A

First, never modify the arguments object directly.
Do it indirectly like so:
var args = Array.slice.call( arguments ) or Array.slice.call( arguments, 1 );

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

What is a the “receiver” of a method call?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How can you use bind( ) to have setTimeout( ) use a particular object for ‘this’?

A

var timer = setTimeout( someObj.method.bind( someObj ), 1000 );

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

How can you use bind( ) to curry a function?

A

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 ]

17
Q

SomeObj.prototype.func = function( arr ) {
return arr.map( function( item ) {
return item + this.prop;
} } // What is the receiver of this?

A

arr, because map function binds it’s callback receiver to its array.

18
Q

Review the arguments accepted by array methods map, filter, some, forEach, and every…

A

map ( callbackFunction, [ thisArg ] )
callback takes item, index, and array.
thisArg is the this-binding for the callback.

19
Q

The scope of ‘this’ is always determined by what?

A

Its nearest enclosing function.

20
Q

How could you remove an item, “myItem”, from an array based on the name of the item?

A
var i = myArray.indexOf( "myItem" );
if ( i >= 0 ) {
    myArray.splice(  i, 1 ); 
}
21
Q

How can we make SubClass( ) a sub class of SuperClass( ) ?

A

SubClass.prototype = Object.create( SuperClass.prototype ); // Better than SubClass.prototype = new SuperClass() because we can’t pass instance arguments and other problems can occur.

22
Q
Sub class inherits from Super and var subby = new Sub( ); 
Object.getPrototypeOf( subby ) === Super.prototype; True or False?
A

False, because getPrototypeOf() returns the internal [[prototype]] of subby, which is Sub.prototype, not Super.prototype.

23
Q
Sub class inherits from Super and var subby = new Sub( ); 
Super.prototype.isPrototypeOf( subby ); 
True or False?
A

True, because isPrototypeOf( ) tells you if the receiver (Super.prototype) is in the prototype chain of subby, which it is.

24
Q
Sub class inherits from Super through prototype chain.
Object.getPrototypeOf( Sub.prototype ) === ???
What will make this true?
A

Super.prototype.

25
Q

How do you check if the value of something is undefined?

A

if ( typeof === “undefined”)…

Note quotes.

26
Q

How do you create a merge/extend function?

A

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.

27
Q

Is the HTML canvas API stateful or stateless?

A

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.

28
Q

How is Array.isArray different from arr instanceof Array and different from Object.prototype.toString( arr ); // “[object Array]” ?

A

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.

29
Q
urls.forEach( function( url, i ) {
// do something asynchronously. Callback includes...
   result[i] = text; 
   if ( result.length === urls.length ) ...
A

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.

30
Q

Why should you never call asynchronous callbacks synchronously?

A

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