tags:

views:

1187

answers:

8

In my particular case:

callback instanceof Function

or

typeof callback == "function"

does it even matter, what's the difference?

+2  A: 

instanceof also works when callback is a subtype of Function, I think

newacct
good point also
farinspace
+13  A: 

Both are essentially identical in terms of functionality, however, I personally prefer instanceof because its comparing types rather than strings. Type comparison is less prone to human error, and its technically faster since its comparing pointers in memory rather than having to do whole string comparisons.

Soviut
On other hand, I think it would have weird problems in multi-window (iframe anyone) setups.
alamar
In that case you'd have to deal with proper namespacing and domains.
Soviut
there are some situations where instanceof will not work as expected and typeof works well ... https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Operators/Special_Operators/Instanceof_Operator#Description
farinspace
instanceof works with objects in the same window. If you use iframe/frame or popup-windows each (i)frame/window have their own "function" object and instanceof will fail if you try to compare an object from another (i)frame/window. typeof will work in all cases since it returns the string "function".
some
+1  A: 

I would recommend using prototype's callback.isFunction().

They've figured out the difference and you can count on their reason.

I guess other JS frameworks have such things, too.

Instanceof wouldn't work on fuctions defined in other windows, I believe. Their Function is different than your window.Function.

alamar
A: 

Coming from a strict OO upbringing I'd go for

callback instanceof Function

Strings are prone to either my awful spelling or other typos. Plus I feel it reads better.

Keplerf1
+2  A: 

instanceof in Javascript can be flaky - I believe major frameworks try to avoid its use. Different windows is one of the ways in which it can break - I believe class hierarchies can confuse it as well.

There are better ways for testing whether an object is a certain built-in type (which is usually what you want). Create utility functions and use them:

function isFunction(obj) {
  return typeof(obj) == "function";
}
function isArray(obj) {
  return typeof(obj) == "object" 
      && typeof(obj.length) == "number" 
      && isFunction(obj.push);
}

And so on.

levik
In case you didn't know: typeof don't need parenthesis since it is a keyword and not a function. And IMHO you should use === instead of ==.
some
@some You are right about typeof but in this case there is no need for ===, it is only needed when the value being compared could equal without having the same type. Here, it can't.
Renesis
@some does typeof ever return something other than a string?
Kenneth J
A: 

Use instanceof because if you change the name of the class you will get a compiler error.

Robert
+2  A: 

A good reason to use typeof is if the variable may be undefined.

alert(typeof undefinedVariable); // alerts the string "undefined"
alert(undefinedVariable instanceof Object); // throws an exception

A good reason to use instanceof is if the variable may be null.

var myNullVar = null;
alert(typeof myNullVar ); // alerts the string "object"
alert(myNullVar  instanceof Object); // alerts "false"

So really in my opinion it would depend on what type of possible data you are checking.

Kenneth J
A: 

Significant practical difference:

var str = 'hello word';

str instanceof String   // false

typeof str === 'string' // true

Don't ask me why.

Barney
Because here `str` is a string primitive, not a string object. The same goes for number primitives and boolean primitives, they aren't instances of their "constructed" counterparts, the String, Number and Boolean objects. JavaScript automatically converts these three primitives to objects when required (such as utilizing a method on the object's prototype chain). On the flip side of your practical difference, *instanceof* is better for checking for arrays since `typeof [] == "object" // true`.
Andy E