奇怪的 javascript 对象问题

发布于 2024-10-10 09:28:36 字数 356 浏览 1 评论 0原文

这是一个简单的 js 测试脚本

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

,如果运行它,您将收到以下错误消息:

Array length must besigned a Finite Positive Number

知道原因是什么以及如何解决克服它吗?

注意:如果将“长度”更改为其他任何内容,它将毫无问题地显示对象的内容。

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 overcome it?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

因为看清所以看轻 2024-10-17 09:28:37

只需添加 Sean 的出色答案,使用 JavaScript,您可以使用点线表示法或括号表示法来访问对象属性。所以:

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

同样

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

,您试图设置长度(字符串)。数组实例的 length 属性很特殊,并且(正如您所发现的)必须是非负的、有限的整数值。


Tangent:事实上,length 属性是数组仅有的三个特殊特性之一(其中之一并不是那么特别)。数组和普通对象之间的唯一区别是:

  1. 它具有一类特殊的属性名称,它们是数字、十进制字符串。我们倾向于将它们称为“数组索引”并将它们写为数字,但事实并非如此。 a[0] 真正的意思是 a["0"] ,它的意思是“对象a的属性'0'”,就像< code>a["foo"] 表示“对象 a 的属性 'foo'。
  2. 特殊的 length 属性:它始终是一个更大的值比具有最高十进制数字字符串名称的属性的数值(例如,最高的“数组索引”)或您设置的值,以较高者为准:

    var a = [];
    a[0] = “foo”; // a.length 自动设置为 1
    a[4] = "酒吧"; // a.length 自动设置为 5
    a.长度=3; // a.length 现在为 3,并且任何具有数字的属性
                  // 删除“3”或更高的十进制字符串名称,因此:
    警报(a[4]); // “不明确的”
    
  3. 数组实例是由 Array 构造函数创建的,因此它们具有原型 Array.prototype 及其所有漂亮有用的函数。

就是这样。它们与几乎所有其他语言中的数组非常不同。例如,它们不是连续的内存块(尽管如果愿意的话,实现可以以这种方式实现它们;这可能效率低下)。

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).

久隐师 2024-10-17 09:28:36

Array.length

Array.lengthArray< 的内置属性/a> 对象。

您只能将其设置为整数值:

长度属性的值为带正号的整数且小于 2 的 32 次方 (232)。

您可以随时设置 length 属性来截断数组。当您通过更改数组的长度属性来扩展数组时,实际元素的数量不会增加;例如,如果当前长度为 2 时将长度设置为 3,则数组仍仅包含 2 个元素。

参考:


Javascript 对象

我的猜测是你想要一个对象,而不是Array

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

当然还有一些替代语法选择:

第一行是以下的快捷方式:

var testobj = new Object();

并且方括号表示法可以替换为点表示法:(

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

当然,方括号和点也可以混合)

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:


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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文