在什么情况下存储在数组中不同的数据类型在Javascript中有用?

时间:2021-09-07 22:24:09

I have been learning some javascript recently and found out that you can store different data types in an array like this:

我最近学习了一些javascript,发现你可以在这样的数组中存储不同的数据类型:

var myArray = [12, 23.5, "hello", true]; 

I have some Java background and this is not possible in Java as you would have to declare the data type otherwise you would get an error (int myArray = blah blah blah)

我有一些Java背景,这在Java中是不可能的,因为你必须声明数据类型,否则你会得到一个错误(int myArray = blah blah blah)

So my question is, in what situations would you use this instead of an object for example. Examples would be great. Thanks.

所以我的问题是,在什么情况下你会使用它代替一个对象。例子很棒。谢谢。

4 个解决方案

#1


4  

This is the case in any language that is not strongly typed. Your array members can be of different primitive and they also can be objects. In most cases you wouldn't want to use this because there is no clear structure to your array. You would rather have something like:

任何非强类型语言都是如此。您的数组成员可以是不同的基元,也可以是对象。在大多数情况下,您不希望使用它,因为阵列没有明确的结构。你宁愿有这样的东西:

var data = {
    prop: 12,
    otherProp: 24.5,
    stringProp: "hello",
    boolProp: true
};

#2


3  

From what I know, there's not really a rule or set of specific situations where it would be considered best practice to store values of different types all in the same array in JavaScript. This is possible just because JavaScript is dynamically typed. I don't think there's any other reason besides that.

据我所知,实际上没有一个规则或一组特定的情况,在JavaScript中将所有不同类型的值存储在同一个数组中被认为是最佳实践。这是可能的,因为JavaScript是动态类型的。我不认为除此之外还有其他任何原因。

Is it best practice to do so? IMHO, I don't think so, really. I think the reason why Java (and I mentioned Java because you said you have some background there) restricts array data types (as do many languages) is partly because it's cleaner and separates concerns. I think restricting variables/other objects in JavaScript to one data type is usually a good idea regardless of the "capability" of dynamic typing.

最佳做法是这样做吗?恕我直言,我不这么认为,真的。我认为Java(我提到Java因为你说你有一些背景)的原因限制了数组数据类型(就像许多语言一样)部分是因为它更清晰并且分离了关注点。我认为将JavaScript中的变量/其他对象限制为一种数据类型通常是一个好主意,无论动态类型的“能力”如何。

Edit

Jonathan Lonowski pointed out that a good application is in Function.prototype.apply, which makes sense. That's because you can pass in an array of data, all of which may have different types. That's a good situation where it would make sense that JavaScript would have dynamic typing in objects like arrays, etc.

Jonathan Lonowski指出,一个好的应用程序在Function.prototype.apply中,这是有道理的。那是因为你可以传入一组数据,所有数据都可能有不同的类型。这是一个很好的情况,JavaScript会在像数组等对象中进行动态类型化是有意义的。

#3


3  

Although it is possible to store different data types in array, it is considered a poor programming style, because an Array by definition is homogeneous data structure.

虽然可以在数组中存储不同的数据类型,但它被认为是一种糟糕的编程风格,因为根据定义,数组是同构数据结构。

How would one handle in uniform way such array:

如何以统一的方式处理这样的数组:

[ 2.5 , [ 1, 2 ], ["a", "b", "c"], {min: 2, max: 5}  ]

for

  • sorting
  • serialization
  • memory utilization
  • algorithms i.e. for maximum value
  • 算法即最大值

It does not seem natural to have different types in array, does it?

在数组中使用不同的类型似乎并不自然,是吗?

#4


2  

Actually, it is posible in Java, you have to declare:

实际上,它在Java中是可行的,你必须声明:

Object[] prove = {1, "hi"};

In Javascript is the same, an array of objects.

在Javascript中是一个对象数组。

#1


4  

This is the case in any language that is not strongly typed. Your array members can be of different primitive and they also can be objects. In most cases you wouldn't want to use this because there is no clear structure to your array. You would rather have something like:

任何非强类型语言都是如此。您的数组成员可以是不同的基元,也可以是对象。在大多数情况下,您不希望使用它,因为阵列没有明确的结构。你宁愿有这样的东西:

var data = {
    prop: 12,
    otherProp: 24.5,
    stringProp: "hello",
    boolProp: true
};

#2


3  

From what I know, there's not really a rule or set of specific situations where it would be considered best practice to store values of different types all in the same array in JavaScript. This is possible just because JavaScript is dynamically typed. I don't think there's any other reason besides that.

据我所知,实际上没有一个规则或一组特定的情况,在JavaScript中将所有不同类型的值存储在同一个数组中被认为是最佳实践。这是可能的,因为JavaScript是动态类型的。我不认为除此之外还有其他任何原因。

Is it best practice to do so? IMHO, I don't think so, really. I think the reason why Java (and I mentioned Java because you said you have some background there) restricts array data types (as do many languages) is partly because it's cleaner and separates concerns. I think restricting variables/other objects in JavaScript to one data type is usually a good idea regardless of the "capability" of dynamic typing.

最佳做法是这样做吗?恕我直言,我不这么认为,真的。我认为Java(我提到Java因为你说你有一些背景)的原因限制了数组数据类型(就像许多语言一样)部分是因为它更清晰并且分离了关注点。我认为将JavaScript中的变量/其他对象限制为一种数据类型通常是一个好主意,无论动态类型的“能力”如何。

Edit

Jonathan Lonowski pointed out that a good application is in Function.prototype.apply, which makes sense. That's because you can pass in an array of data, all of which may have different types. That's a good situation where it would make sense that JavaScript would have dynamic typing in objects like arrays, etc.

Jonathan Lonowski指出,一个好的应用程序在Function.prototype.apply中,这是有道理的。那是因为你可以传入一组数据,所有数据都可能有不同的类型。这是一个很好的情况,JavaScript会在像数组等对象中进行动态类型化是有意义的。

#3


3  

Although it is possible to store different data types in array, it is considered a poor programming style, because an Array by definition is homogeneous data structure.

虽然可以在数组中存储不同的数据类型,但它被认为是一种糟糕的编程风格,因为根据定义,数组是同构数据结构。

How would one handle in uniform way such array:

如何以统一的方式处理这样的数组:

[ 2.5 , [ 1, 2 ], ["a", "b", "c"], {min: 2, max: 5}  ]

for

  • sorting
  • serialization
  • memory utilization
  • algorithms i.e. for maximum value
  • 算法即最大值

It does not seem natural to have different types in array, does it?

在数组中使用不同的类型似乎并不自然,是吗?

#4


2  

Actually, it is posible in Java, you have to declare:

实际上,它在Java中是可行的,你必须声明:

Object[] prove = {1, "hi"};

In Javascript is the same, an array of objects.

在Javascript中是一个对象数组。