Closed Bug 430771 Opened 16 years ago Closed 16 years ago

window.setTimeout passes spurious argument to function unless wrapped in a closure

Categories

(Firefox :: General, defect)

x86
Windows XP
defect
Not set
normal

Tracking

()

RESOLVED DUPLICATE of bug 394769

People

(Reporter: dra-news, Unassigned)

Details

User-Agent:       Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648)
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14

The following script should produce two identical messages (a message with "Arguments length: 0"). The first instance passes a spurious (and seemingly random) integer value in the arguments array where the second call (wrapped in a closure) works.

function myTest()
{
  alert('Arguments length: ' + arguments.length);
  for (var i = 0; i < arguments.length; i++)
  {
    alert('Argument ' + (i + 1) + ' is ' + arguments[i]);
  }
}

window.setTimeout(myTest, 100);
window.setTimeout(function () {myTest();}, 100);


Reproducible: Always

Steps to Reproduce:
1. Insert the script above into a <script> tag in an HTML file
2. Load the page
Actual Results:  
First message is "Arguments length: 1" followed by "Argument 1 is x" (x is a random integer) and then "Arguments length: 0" for the second call.

Expected Results:  
Message "Arguments length: 0" both times.

Safari 3.1 and IE 7 don't exhibit this behaviour so guessing that it's a bug rather than a subtle DOM feature.
First one is window.setTimeout("myTest()", 100);
Then, both get message "Arguments length: 0".

-> Duplicate of bug 10637
(In reply to comment #2)
> -> Duplicate of bug 10637

I certainly regard this as a bug (given that #10637 asks whether anyone has found it a problem) and so not a duplicate - the behaviour is not specified or consistent and I spent two hours debugging the fault to discover that it was Firefox's inconsistent behaviour. The function I was calling is used in other contexts and behaves differently if passed an argument (i.e. it's overloaded). Given that my call to setTimeout passes no argument, it took a lot of debugging to discover that this bizarre behaviour was in fact causing the fault.
(In reply to comment #1)
> First one is window.setTimeout("myTest()", 100);
> Then, both get message "Arguments length: 0".

Except that the adjustment creates style-free JavaScript - the implied use of eval in "myTest()" is shoddy and never necessary in any well-written code intended for a modern JavaScript interpreter.
Fine, since you don't like the decision in bug 10637 I'll mark it as a dupe of 394769.
Status: UNCONFIRMED → RESOLVED
Closed: 16 years ago
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.