最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

In what situation would storing in an array different data types be useful in Javascript? - Stack Overflow

programmeradmin1浏览0评论

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

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)

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

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

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)

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

Share Improve this question edited Nov 21, 2015 at 13:14 Felipe Alarcon asked Jun 10, 2015 at 15:37 Felipe AlarconFelipe Alarcon 95611 silver badges19 bronze badges 2
  • 1 One example would be an array of arguments, as given to .apply(). – Jonathan Lonowski Commented Jun 10, 2015 at 15:41
  • 1 It's all like ArrayList<Object> in Java. – Bergi Commented Jun 10, 2015 at 15:42
Add a ment  | 

5 Answers 5

Reset to default 5

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
};

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?

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.

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.

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.

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

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

In Javascript is the same, an array of objects.

Javascript consider arrays as an object, ie:arrays are special type of objects in Javascript

But the difference is objects in javascript uses names to access elements

  1: var PM = {firstName:"Narendra", lastName:"Modi", age:70};

  2:  PM.firstName returns Narendra.

But arrays of javascript numbered indexes.

  1: var fruits = ["Banana", "Orange", "Apple", "Mango"];

  2: fruits[2] returns Apple.

by using collections we can resemble Javascript's array properties in JAVA too.that is by using List in java you can able to store different data types in an array ie:arraylist.

发布评论

评论列表(0)

  1. 暂无评论