阵列键编号和“编号"被意外地认为是一样的 [英] Arrays keys number and "number" are unexpectedly considered the same

查看:64
本文介绍了阵列键编号和“编号"被意外地认为是一样的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用javascript数组,遇到了一些不一致之处,希望有人可以帮我解释一下.

I have been playing with javascript arrays and I have run into, what I feel, are some inconsistencies, I hope someone can explain them for me.

让我们以此开始:


var myArray = [1, 2, 3, 4, 5];
document.write("Length: " + myArray.length + "<br />");
for( var i in myArray){
   document.write( "myArray[" + i + "] = " + myArray[i] + "<br />");
}
document.write(myArray.join(", ") + "<br /><br />");


Length: 5
myArray[0] = 1
myArray[1] = 2
myArray[2] = 3
myArray[3] = 4
myArray[4] = 5
1, 2, 3, 4, 5

这段代码没有什么特别的,但是我了解到javascript数组是一个对象,因此可能将特性添加到该数组中,这些特性添加到数组的方式对我来说似乎不一致.

There is nothing special about this code, but I understand that a javascript array is an object, so properities may be add to the array, the way these properities are added to an array seems inconsistent to me.

在继续之前,让我注意一下如何在javascript中将字符串值转换为数字值.

Before continuing, let me note how string values are to be converted to number values in javascript.

  • 非空字符串->字符串或NaN的数值

  • Nonempty string -> Numeric value of string or NaN

空字符串-> 0

Empty string -> 0

因此,由于javascript数组是一个对象,因此以下内容是合法的:

So since a javascript array is an object the following is legal:


myArray["someThing"] = "someThing";
myArray[""] = "Empty String";
myArray["4"] = "four";

for( var i in myArray){ document.write( "myArray[" + i + "] = " + myArray[i] + "<br />"); } document.write(myArray.join(", ") + "<br /><br />");

for( var i in myArray){ document.write( "myArray[" + i + "] = " + myArray[i] + "<br />"); } document.write(myArray.join(", ") + "<br /><br />");


Length: 5
myArray[0] = 1
myArray[1] = 2
myArray[2] = 3
myArray[3] = 4
myArray[4] = four
myArray[someThing] = someThing
myArray[] = Empty String
1, 2, 3, 4, four

输出意外.

设置属性myArray ["4"]时,非空字符串"4"转换为数值,这似乎是正确的.但是,空字符串"不会转换为数值0,它将被视为空字符串.同样,非空字符串"something"也不会转换为数值NaN,它被视为字符串.那是什么呢?数字或字符串上下文中的myArray []内的语句是什么?

The non empty string "4" is converted into its numeric value when setting the property myArray["4"], this seems right. However the empty string "" is not converted into its numeric value, 0, it is treated as an empty string. Also the non empty string "something" is not converted to its numeric value, NaN, it is treated as a string. So which is it? is the statement inside myArray[] in numeric or string context?

此外,为什么myArray的两个非数字性质不包含在myArray.length和myArray.join(,")中?

Also, why are the two, non numeric, properities of myArray not included in myArray.length and myArray.join(", ")?

推荐答案

JavaScript数组的键实际上是字符串.有关任意键的映射类型的详细信息和实现,请查看此答案.

The keys of a JavaScript array are actually strings. For details and an implementation of a map type for arbitrary keys, check this answer.

澄清和补充Jason发表的内容:JavaScript数组是对象.对象具有属性.属性名称是一个字符串值.因此,数组索引也将转换为字符串,然后再进行其他操作.如果满足以下条件(ECMA-262,15.4),则将属性名称P视为数组索引(即,将调用特殊的数组魔术师):

To clarify and add to what Jason posted: JavaScript arrays are objects. Objects have properties. A property name is a string value. Therefore, array indices are converted to strings as well before anything more can happen. A property name P will be treated as an array index (ie the special array-magic will be invoked) if the following holds (ECMA-262, 15.4):

ToString(ToUint32(P))等于P而ToUint32(P)不等于2 ^ 32 − 1

ToString(ToUint32(P)) is equal to P and ToUint32(P) is not equal to 2^32 − 1

可以很容易地验证将数字索引转换为字符串(而不是相反)的情况:

That numeric indices will be converted to strings (and not the other way around) can be easily verified:

var array = [];
array[1] = 'foo';
array['1'] = 'bar';
array['+1'] = 'baz';
document.writeln(array[1]); // outputs bar


此外,用for..in循环遍历数组条目的坏习惯是-如果有人弄乱了一些原型,您可能会得到意想不到的结果(而且也不是很快).请使用标准的for(var i= 0; i < array.length; ++i).


Also, its bad practice to iterate over an array's entries with a for..in loop - you might get unexpected results if someone messed with some prototypes (and it's not really fast, either). Use the standard for(var i= 0; i < array.length; ++i) instead.

这篇关于阵列键编号和“编号"被意外地认为是一样的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆