this - Different behavior for calling function on non-object in javascript -


both firefox's js engine/gjs (spidermonkey) , webkit's jscore seems have different behavior when calling builtin , normal js functions on non-object.

gjs> tostring.call(null) "[object null]" gjs> function cccc() {return tostring.call(this);} gjs> cccc.call(null) "[object gjsglobal]" 

so if normal js function called non-object, this automatically replaced current this, whereas won't happen builtin function.

is standard behavior (according specification?) or implementation dependent behavior? first line safe check type of value?

thx

that behaviour design. noted in mozilla documentation:

note may not actual value seen method: if method function in non-strict mode code, null , undefined replaced global object, , primitive values boxed.

apparently, builtin functions (treated as) strict mode code. own functions not, unless, of course, write strict mode function. :-)

this different in ecmascript 5. specification (pdf):

note thisarg value passed without modification this value. change edition 3, undefined or null thisarg replaced global object , toobject applied other values , result passed value.

here’s more direct link non-normative html version of spec.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -