What is a JavaScript Array? - Stack Overflow

I'm trying to understand what a JavaScript array is because traditional programming languages defi

I'm trying to understand what a JavaScript array is because traditional programming languages define an array as a contiguous area of storage that can be addressed using an offset.

Now, a normal JavaScript object can be addressed as:

myObj.myProperty = "my Value";

or

myObj["myProperty"] = "my Value";

So, a JavaScript array is simply using numbers instead of names in it's addressing:

myObj[0] = "my Value";
myObj.length // === 1

A JavaScript Array also has methods, such as slice(), and join().

Q: Is what I said so far true?

I'm trying to understand what a JavaScript array is because traditional programming languages define an array as a contiguous area of storage that can be addressed using an offset.

Now, a normal JavaScript object can be addressed as:

myObj.myProperty = "my Value";

or

myObj["myProperty"] = "my Value";

So, a JavaScript array is simply using numbers instead of names in it's addressing:

myObj[0] = "my Value";
myObj.length // === 1

A JavaScript Array also has methods, such as slice(), and join().

Q: Is what I said so far true?

Share Improve this question asked Jun 22, 2011 at 18:53 Phillip SennPhillip Senn 47.7k91 gold badges261 silver badges378 bronze badges 2
  • 3 @harper89: w3fools. – Naftali Commented Jun 22, 2011 at 18:57
  • 1 Slight correction on one point: "a JavaScript array is simply using numbers instead of names in it's addressing" -- it's actually still using strings. When you type myObj[0], it gets converted to myObj['0'] – user578895 Commented Jun 22, 2011 at 19:05
Add a ment  | 

5 Answers 5

Reset to default 5

A JavaScript array is a hash object with array functions attached using Array.prototype. Put simply, this is an "Array" in JavaScript:

var x = {
    length : 3,
    '0'    : 'first',
    '1'    : 'second',
    '2'    : 'third'
};
x.__proto__ = Array.prototype;

All of the array functions only act on indexes, as you would expect, however you can also do anything to an array object that you would do to a general JS object:

ary.foo = 'bar';

To a basic yes or no question: Yes all of what you said is true.

Here is a whole array tutorial

Javascript objects are associative arrays. Javascript has an Object called Array that has special methods for dealing with their data.

a good read ( that got me going at start ) Mastering Javascript Arrays

Any JavaScript array is an object that can use different objects* as keys, making it a hash.

*all objects different from strings will be converted to string [object Object], so they will act as the same key! (thanks to cwolves :)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744714587a4589540.html

相关推荐

  • What is a JavaScript Array? - Stack Overflow

    I'm trying to understand what a JavaScript array is because traditional programming languages defi

    17小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信