'Is JavaScript Object nothing but an associative array?

Ok am just going through basics of JavaScript and I was learning objects where I came across this example...

JavaScript

var person = {
   firstname : "Smith",
   lastname  : "Bach"
};

And what we write in PHP is

$person = array(
    "firstname"=>"Smith", 
    "lastname"=>"Bach"
);

So is this the same thing or am making a mistake in understanding the concept?



Solution 1:[1]

They are associative arrays, but not just associative arrays. There are functions available from the Object prototype (like .toString()) whose names can collide with property names. Objects can be constructed via other functions and given more inherited properties too. (Note that one thing that plain objects don't have is a .length property to count entries, like array objects have. The term "associative array" is probably not the best one to use for JavaScript objects; they're objects and that should be enough once you're familiar with JavaScript.)

edit — what I mean is this:

var o = {};
alert("toString" in o); // alerts "true"

Thus a newly-created empty object appears to have a property called "toString". The issue with JavaScript is that there's only one property accessor operator (well two, but they're two flavors of the same thing), so there's no way to distinguish between accesses to the array's contents and access to the array's API. (Also, in JavaScript it's really not a good idea to think of them using the word "array", as that means something different in JavaScript — arrays are a type of Object with special properties.)

EcmaScript 5 has mechanisms for defining object properties in such a way as to make them immutable and non-iterable, which helps some. It's still problematic if you want to store a property called "toString" in an object.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1