Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript toString quirk

Tags:

javascript

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

like image 737
Justin Swartsel Avatar asked Dec 23 '22 07:12

Justin Swartsel


1 Answers

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.

like image 147
user187291 Avatar answered Feb 08 '23 18:02

user187291