Justin Swartsel
Justin Swartsel

Reputation: 3431

Javascript toString quirk

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()".

Upvotes: 4

Views: 403

Answers (1)

user187291
user187291

Reputation: 53940

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.

Upvotes: 11

Related Questions