JavaScript数组索引是字符串还是整数? [英] Is JavaScript array index a string or an integer?

查看:130
本文介绍了JavaScript数组索引是字符串还是整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于JavaScript数组的一般性问题。 JavaScript中的数组索引是否作为字符串内部处理?我读到某处,因为数组是JavaScript中的对象,索引实际上是一个字符串。我对此感到有点困惑,并且对任何解释都很高兴。

I had a generic question about JavaScript arrays. Are array indices in JavaScript internally handled as strings? I read somewhere that because arrays are objects in JavaScript, the index is actually a string. I am a bit confused about this, and would be glad for any explanation.

推荐答案

这是正确的:

> var a = ['a','b','c']
undefined
> a
[ 'a', 'b', 'c' ]
> a[0]
'a'
> a['0']
'a'
> a['4'] = 'e'
'e'
> a[3] = 'd'
'd'
> a
[ 'a', 'b', 'c', 'd', 'e' ]

这篇关于JavaScript数组索引是字符串还是整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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