奇怪的 javascript 对象问题
这是一个简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需添加 Sean 的出色答案,使用 JavaScript,您可以使用点线表示法或括号表示法来访问对象属性。所以:
同样
,您试图设置长度(字符串)。数组实例的
length
属性很特殊,并且(正如您所发现的)必须是非负的、有限的整数值。Tangent:事实上,
length
属性是数组仅有的三个特殊特性之一(其中之一并不是那么特别)。数组和普通对象之间的唯一区别是:a[0]
真正的意思是a["0"]
,它的意思是“对象a
的属性'0'”,就像< code>a["foo"] 表示“对象a
的属性 'foo'。特殊的
length
属性:它始终是一个更大的值比具有最高十进制数字字符串名称的属性的数值(例如,最高的“数组索引”)或您设置的值,以较高者为准:就是这样。它们与几乎所有其他语言中的数组非常不同。例如,它们不是连续的内存块(尽管如果愿意的话,实现可以以这种方式实现它们;这可能效率低下)。
Just to add to Sean's excellent answer, with JavaScript, you can use either dotted notation or bracketed notation to access object properties. So:
and similarly
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:a[0]
really meansa["0"]
which means "the property '0' of the objecta
", just likea["foo"]
means "the property 'foo' of the objecta
.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:Array
constructor function, and so they have the prototypeArray.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).
Array.length
Array.length
是Array
< 的内置属性/a> 对象。您只能将其设置为整数值:
参考:
Array.length< /code>
(Mozilla JavaScript 参考)
Javascript 对象
我的猜测是你想要一个
对象
,而不是Array
:当然还有一些替代语法选择:
第一行是以下的快捷方式:
并且方括号表示法可以替换为点表示法:(
当然,方括号和点也可以混合)
Array.length
Array.length
is a built-in property of theArray
object.You can only set it to an integer value:
Reference:
Array.length
(Mozilla JavaScript Reference)Javascript Objects
My guess is that you want an
Object
, not anArray
:There are of course some alternative syntax choices:
The first line is a shortcut for:
And the square brackets notation can be replaced with the dot notation:
(Square brackets and dots can also be mixed, of course)