tags:

views:

92

answers:

1

Suppose I have a simple function defined that does nothing: function fn() { }

Now, when I run toString(fn) I get "[object Object]". When I run toString.call(fn) I get "[object Function]". Does anyone know why I get a more specific type when using the call method?

EDIT: This behavior is exhibited in FireFox run through FireBug console. Both toString.constructor and toString.call.constructor yield "Function()".

+9  A: 

toString doesn't accept arguments, so toString(fn) is the same as just toString(), which returns an implicit global object, converted to string. toString.call(fn) calls global.toString passing function object as this, but since global.toString is a method of Object, the result is different from Function.toString.

stereofrog
+1 for revealing the "implicit global object"!
Jonathan Feinberg
Thanks also for pointing out the obvious flaw in my experiment: I forgot that I wasn't passing fn as a parameter into call but as the context! Now it all makes total sense.
Justin Swartsel