Reputation: 2053
I am a novice in Basic JavaScript Concepts so want to understand how is this possible.
I have an object x
var x={}
and it has a propery y
whose value is an array
x.y=["a","b","c"]`
so its like a map where y
is the key and array["a","b","c"]
is the value.then how can y
hold another property called z
which has some other value say "hi"
x.y.z= "hi"
y
is already ["a","b","c"]
-------------------------> x.y= ["a","b","c"]
how can it be {"z":"hi"}
then------------------------>x.y={"z":"hi"}
and if you expand x
this is how it is shown
To have a clear picture on this ,which basic concepts i should be familiar with?
Upvotes: 0
Views: 65
Reputation: 7985
In javascript, arrays are objects. For example, if your check the "type" for each:
var myArray = [];
var myObject = {};
console.log(typeof(myArray)); // yields 'object'
console.log(typeof(myObject)); // yields 'object'
The idea is that an array is built upon the lower-level "object" (Arrays extend Object) by adding additional functionality, such as the "length" property, join, sort, etc.
So when you manually put a property "into" an array it will honor your request because Array is an extension of Object, so and Array can do everything that an object can do.... and more!
So for this example:
// Our traditional array
var myArray = ["aaa", "bbb", "ccc"];
We'd expect the normal indicing, where:
myArray[0] -> aaa
myArray[1] -> bbb
myArray[2] -> ccc
But when we manually put a property into the array:
myArray.xyz = "interesting";
Well then, it can and will get stored in the array.
If you think about an array for a second, you can access it's "length" property, right?
var len = myArray.length;
So there are a bunch of properties already within each array, so when you add your own property, that's ok (although not recommended), because javascript is quite flexible.
That proto thing you see is showing how "things" in javascript can "extend". Again, Array extends Object. In javascript Prototype is kinda like the "parent" class that other classes can build upon -- or extend -- and incorporate more functionality.
So you'll see that there is an "Object" proto in there, which means that "Array" is leveraging the "Object's" Prototype (so it can do everything that an object can do), then it adds a bunch of stuff to the prototype and the result is the Array Prototype, which is now it's own entity, complete with different functionality beyond that which the Object had.
You'll hear from time to time folks say that Javascript is a "prototype language". And your browser shows you how the prototyping is structured if you dig down into the rabbit hole.
Upvotes: 0
Reputation: 386550
Because arrays are object withe some special methods.
Arrays are list-like objects whose prototype has methods to perform traversal and mutation operations. Neither the length of a JavaScript array nor the types of its elements are fixed. Since an array's size length grow or shrink at any time, JavaScript arrays are not guaranteed to be dense. In general, these are convenient characteristics; but if these features are not desirable for your particular use, you might consider using typed arrays.
Some people think that you shouldn't use an array as an associative array. In any case, you can use plain objects instead, although doing so comes with its own caveats. See the post Lightweight JavaScript dictionaries with arbitrary keys as an example.
Upvotes: 1