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

RESOLVED DUPLICATE of bug 394769

Status

()

defect
RESOLVED DUPLICATE of bug 394769
11 years ago
11 years ago

People

(Reporter: dra-news, Unassigned)

Tracking

Firefox Tracking Flags

(Not tracked)

Details

(Reporter)

Description

11 years ago
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.

Comment 1

11 years ago
First one is window.setTimeout("myTest()", 100);
Then, both get message "Arguments length: 0".

Comment 2

11 years ago
-> Duplicate of bug 10637
(Reporter)

Comment 3

11 years ago
(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.
(Reporter)

Comment 4

11 years ago
(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.

Comment 5

11 years ago
Fine, since you don't like the decision in bug 10637 I'll mark it as a dupe of 394769.
Status: UNCONFIRMED → RESOLVED
Last Resolved: 11 years ago
Resolution: --- → DUPLICATE
Duplicate of bug: 394769
You need to log in before you can comment on or make changes to this bug.