admin管理员组

文章数量:1333400

Here's a simple js test script

<script>
var testarray = new Array();
testarray['length'] = "1,2,3,4,5";
alert(testarray['length']);
</script>

If you run it, you will get the following error message:

Array length must be assigned a finite positive number

Any idea what's the reason and how to overe it?

Note: if you change 'length' to anything else, it will show the content of the object with no issue.

Here's a simple js test script

<script>
var testarray = new Array();
testarray['length'] = "1,2,3,4,5";
alert(testarray['length']);
</script>

If you run it, you will get the following error message:

Array length must be assigned a finite positive number

Any idea what's the reason and how to overe it?

Note: if you change 'length' to anything else, it will show the content of the object with no issue.

Share Improve this question asked Jan 4, 2011 at 10:05 JudyJudy 7931 gold badge8 silver badges9 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 10

Array.length

Array.length is a built-in property of the Array object.

You can only set it to an integer value:

The value of the length property is an integer with a positive sign and a value less than 2 to the 32 power (232).

You can set the length property to truncate an array at any time. When you extend an array by changing its length property, the number of actual elements does not increase; for example, if you set length to 3 when it is currently 2, the array still contains only 2 elements.

Reference:

  • Array.length (Mozilla JavaScript Reference)

Javascript Objects

My guess is that you want an Object, not an Array:

var testobj = {};
testobj['length'] = "1,2,3,4,5";
alert(testobj['length']);

There are of course some alternative syntax choices:

The first line is a shortcut for:

var testobj = new Object();

And the square brackets notation can be replaced with the dot notation:

testobj.length = "1,2,3,4,5";
alert(testobj.length);

(Square brackets and dots can also be mixed, of course)

Just to add to Sean's excellent answer, with JavaScript, you can use either dotted notation or bracketed notation to access object properties. So:

var a = [];          // Same thing as a = new Array();
alert(a.length);     // alerts 0
alert(a['length']);  // ditto
var x = 'len';
alert(a[x + "gth"]); // ditto

and similarly

var a = [];
a.length = 3;        // That's fine, the array's length is now 3
a['length'] = 4;     // And now it's four
var x = 'len';
a[x + "gth"] = 5;    // And now it's five

So you were trying to set the length (to a string). The length property of array instances is special, and (as you discovered) must be a non-negative, finite, integer value.


Tangent: In fact, the length property is one of only three special things about arrays (one of which isn't really all that special). The only differences between an array and a plain object are:

  1. It has a special class of property names that are numeric, decimal strings. We tend to call them "array indexes" and write them as numbers, but they're not. a[0] really means a["0"] which means "the property '0' of the object a", just like a["foo"] means "the property 'foo' of the object a.
  2. The special length property: It's always one greater than the numeric value of the property with the highest decimal numeric string name (e.g., the highest "array index"), or the value you set, whichever is higher. So:

    var a = [];
    a[0] = "foo"; // a.length is automatically set to 1
    a[4] = "bar"; // a.length is automatically set to 5
    a.length = 3; // a.length is now 3, and any properties that have numeric
                  // decimal string names of "3" or higher are deleted, thus:
    alert(a[4]); // "undefined"
    
  3. Array instances are manufactured by the Array constructor function, and so they have the prototype Array.prototype with all of its nifty useful functions.

That's it. They're pretty much unlike arrays in nearly every other language. They aren't, for instance, contiguous blocks of memory (although an implementation can implement them that way if it likes; it would likely be inefficient).

本文标签: Strange javascript object issueStack Overflow