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,undefinedreplaced 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 ,
toobjectapplied other values , result passed value.
here’s more direct link non-normative html version of spec.
Comments
Post a Comment