var arguments; in function body ignored in IE and FireFox

(Warning: this post documents a very obscure and technical quirk. I won't even try to explain it in popular terms. ECMAScript geeks only. 🙂 )

Opera can not enter the KLM website. The reason is a peculiar ECMAScript interpretation corner case quirk.

The issue is code like the excerpt below:

function (){
  var arguments;
  arguments['foo']='bar';
}

Now, Opera allows overwriting the function's arguments property with a local variable set to undefined. For some reason, FF and IE both ignore the "var arguments;" statement. This makes Opera throw an exception because "arguments" is the value undefined when you reach the line where you add properties, the others luckily escape this error and the site works for them.

Who is right? Most likely Opera. According to the ECMAScript spec you can overwrite the "arguments" array inside a function. It works cross-browser to say "var arguments=foo;", it is just overwriting it with nothing that seems to be incorrectly implemented in FireFox and IE.

I guess we may have to follow them and be bug-compatible.

For KLM the simple fix would be to say

var arguments={};

instead, because that's what they actually meant.

Advertisements

5 thoughts on “var arguments; in function body ignored in IE and FireFox

  1. One of the developers says we are probably wrong according to the standard. Cool, so we can fix it and be compatible with the posse AND the standards! Nothing is better! 🙂

  2. I think Opera shouldn't give up the spec in this case. That's just one buggy site, not the majority on the Internet! One site can be fixed with user JS.

  3. Er, you're misunderstanding the var statement. If used without an initializer, if the variable doesn't exist yet, assign it the value undefined. So "var a = 10; var a; alert(a);" outputs "10" instead of "undefined".From the spec:10.1.3 Variable Instantiation…For each VariableDeclaration or VariableDeclarationNoIn in the code, create a property ofthe variable object whose name is the Identifier in the VariableDeclaration orVariableDeclarationNoIn, whose value is undefined and whose attributes are determined bythe type of code. If there is already a property of the variable object with the name of adeclared variable, the value of the property and its attributes are not changed.

  4. Originally posted by Majan:

    you're misunderstanding the var statement. If used without an initializer, if the variable doesn't exist yet

    Yes, it's the "if the variable doesn't already exist" part I had forgotten about. Good thing I'm in QA and not a developer, and the developers got it right and pointed out to me that we are wrong. Hence, we'll fix it :)Unfortunately, KLM still requires a browser.js patch because they use capturing event listeners where they meant to use non-capturing. Here we follow the spec better than FireFox..

  5. Hmm, I thought "arguments" was a keyword just like "this" and not a variable, and thus Opera being right.After having a look in the spec I see I was wrong.Reading [ECMA-262 3ed] 11.1.1 The this Keyword, you can see a special case for "this", it evaluates to the "this" value of the execution context.But "arguments" is not special cased and is not a value of the execution context, it's a property of the activation object ([ECMA-262 3ed] 10.1.6 Activation Object). Which is used as the variable object.So, "arguments" is effectively a local variable with a default value of the Arguments object.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s