当使用一个对象或在JavaScript中的数组? [英] When to use an object or an array in javascript?

查看:112
本文介绍了当使用一个对象或在JavaScript中的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚发现阵列直接从对象在JavaScript中继承。我发现一个数组之间的差异和对​​象是相当微不足道的。

I just found out that Arrays inherit directly from Object in javascript. I'm finding the difference between an array and an object is fairly minuscule.

我怎么知道什么时候使用一个比其他?

How do i know when to use one over the other?

推荐答案

我会使用 []数组,当我处理对象相同的列表 *类型(如字符串列表),我想要的顺序和对象{}当我处理,我想的(属性和东西的属性是不同的类型,通常情况下)。

I'd use an Array [] when I'm dealing with a list of objects the same type* (like a list of strings) that I want the order of and an Object {} when I'm dealing with something that I want properties of (and the properties are of different types, usually).

例如:

var numberNames = ["one","two","three","four"];

var computer = {
     motherBoard : "mother", 
     psu : psu_object(),
     ram : { 
             ram1 : GB,
             ram2 : TwoGB
     }
};

*这只是我的preference。阵列可以包含多个不同类型的元素。

* This is just my preference. Arrays can contain elements of multiple different types.

这篇关于当使用一个对象或在JavaScript中的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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