quirky arguments

Hallvord R. M. Steen hallvord at opera.com
Mon Jan 22 06:58:26 PST 2007


Question from a blog post (URL below):

Here is the issue in a minimal snippet of JavaScript:

function test(){}
test.arguments='something';
test();
alert(test.arguments); // test.arguments is now null ??

The local variable called "arguments" also becomes a property of the  
function itself, and is subsequently reset to null. Consider

function test(){ alert(arguments==test.arguments); }

which says "true" and shows us that the local variable and the function  
property refer to the same object.

Some side-effects of this implementation include:

"arguments" is a reserved word as function property name, it can not be  
used (this isn't documented AFAIK)

We can work out whether a function has been called by a script or not!  
Consider
Function.prototype.arguments=true;
function test(){};
.
. /* function definitions and calls here */
.
var testWasCalled = ! test.arguments;

"testWasCalled" is now true if and only if the script has used the "test"  
function during its execution. Might be a handy trick for trying to  
optimise large scripts

..but where in the ECMA-262 spec is this specified?
In other words, is it a (cross-browser) bug or a feature?

<URL: http://my.opera.com/hallvors/blog/2007/01/22/quirky-arguments >

-- 
Hallvord R. M. Steen
Core QA JavaScript tester, Opera Software
http://www.opera.com/
Opera - simply the best Internet experience




More information about the Es4-discuss mailing list