Title, the original has been mistakenly thought JS inside can not be like PHP definition associative array, such as a["color"]=red;
But a few days ago, I found it possible.
Would you like to ask if this is a more efficient way of defining than manipulating objects? Is the background of the two implementations inconsistent?
Defining objects
Obj={color: "Red", Name: "Apple"};
Associative arrays
Arr=[];
arr["Color"]= "red"; arr["name"]= "Apple";
Obj=[];obj.push (arr);
Reply content:
Title, the original has been mistakenly thought JS inside can not be like PHP definition associative array, such as a["color"]=red;
But a few days ago, I found it possible.
Would you like to ask if this is a more efficient way of defining than manipulating objects? Is the background of the two implementations inconsistent?
Defining objects
Obj={color: "Red", Name: "Apple"};
Associative arrays
Arr=[];
arr["Color"]= "red"; arr["name"]= "Apple";
Obj=[];obj.push (arr);
The number of Javascript is also the same, and at the same time any pair can also be used as a group, detailed below.
See: Http://jayconrod.com/posts/52/a-tour-of-v8-object-representation
Detailed interpretation of the V8 of the image of the reality.
The image is divided into two layers:
The pattern has array mode and dictionary mode. The general situation uses the array mode, which sometimes falls down.
Named Properties
In-object Properties uses a public map to describe its structure, and some situations (difficult to share map) will be downgraded to Dictionary mode.
V8 can handle minor divergences like this just fine, but if your code assigns all sorts of the random properties to objects fr Om the same constructor in no particular order, or if you delete properties, V8 'll drop the object into dictionary mode, The where properties is stored in a hash table. This prevents a absurd number of maps from being allocated.
A process called IN-OBJECT slack tracking is used to determine the size of the image, and the subsequent properties of the extra properties are stored using individual numbers.
Now I ' m sure your next question are, "What happens if a new property was added after IN-OBJECT slack tracking are complete?" "This was handled by allocating a overflow array to store the extra properties. The overflow array can always is reallocated with a larger size as new properties is added.
Numbered properties
Among them, fast elements includes
Fast small integers
Fast Doubles
Fast values
However, fast elements can sometimes be downgraded to dictionary mode
If you assign to a index that's the past the end of your The elements array, V8 may downgrade the elements to dictionary Mode.
Of course, the level does not affect the other types of nature because it is stored separately.
Because named properties and elements is stored separately, even if an object drops to dictionary mode for elements, NA Med properties may still is accessed quickly (and vice versa).
CI, using an array as a associative array does not increase performance.
The amount ... At a shallow level, the following 2 are ' equivalent ':
- Obj.attr = value
- obj[' attr ']=value
Only 2 inside support obj[' attr ' +var]=value, can be more flexible, such as a circular statement inside.
Console try it.
arr=[];arr["color"]="red";arr["name"]="apple";console.log(arr);console.log(arr.length);
There's no difference between doing this and using objects.
In JavaScript, an object is an associative array, and an associative array is an object. The JavaScript authoritative Guide Objects section has said.