Closed Bug 305103 Opened 19 years ago Closed 19 years ago

Array returns undefined instead of the correct value. Using an Array of Functions

Categories

(Core :: JavaScript Engine, defect)

x86
Windows XP
defect
Not set
major

Tracking

()

RESOLVED INVALID

People

(Reporter: taylormau, Unassigned)

Details

User-Agent:       Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)
Build Identifier: SpiderMonkey 

Sample 1 which does not work:

function array_max( ){
   var i, max = this[0];
   for (i = 1; i < this.length; i++)
   {
   if (max < this[i])
   max = this[i];
   }
   return max;
}

Array.prototype.max = array_max;
var w = new Array(1,2,3,4,5,6);
var x = w.max()
var y = new Array(x); // Array with 1 item
var z = y[0];
print(z) // This line returns undefined


Sample 2 which does work:

function array_max( ){
   var i, max = this[0];
   for (i = 1; i < this.length; i++)
   {
   if (max < this[i])
   max = this[i];
   }
   return max;
}

Array.prototype.max = array_max;
var w = new Array(1,2,3,4,5,6);
var x = w.max()
var y = new Array(x, 2); // *THE ONLY CHANGE* Add any other item in addition 
to x
var z = y[0];
print(z) // Returns the correct value 6
		

Reproducible: Always

Steps to Reproduce:
1. Described in the Details section
2.
3.
This bug is INVALID. You're passing a single integer to the array constructor,
which constructs a new array of length N (or in your case, x). Therefore, your
new array[0] is undefined, since you haven't set it to anything. A simple
workaround would be to have:
y = new Array();
y[0] = x;

The reason your second example works is that you are ending up in the version of
the array constructor that takes N items as the array. However, this constructor
is only called if thee number of arguments to the constructor is at least 2.
Status: UNCONFIRMED → RESOLVED
Closed: 19 years ago
Resolution: --- → INVALID
You need to log in before you can comment on or make changes to this bug.