JavaScript arrays and JSON data in objects

Source: Internet
Author: User
Tags scalar

For example, the following user object:

The code is as follows Copy Code
function user (n, a)
{
THIS.name = n;
This.age = A;
this.tostring = function () {
Return "Name:" + THIS.name + ", Age:" + this.age;
}
}
var u = new User ("Tom", 18);
For (var k in u) {
Alert (' key: ' + K + ', Value: ' + u[k]);
}

To make a slight change, use the array object instead of the object:

The code is as follows Copy Code
var user = new Array ();
user["Name"]= "Tom";
user["Age"]=18;
user["ToString"]=function () {
Return "Name:" + THIS.name + ", Age:" + this.age;
}
Alert (user.tostring ());

A simpler formulation:

The code is as follows Copy Code
var u = {
"Name": "Tom",
"Age": 18
}
u.tostring = function () {return ' Name: ' + THIS.name + ', Age: ' + this.age;};
Alert (u.tostring ());

As you can see from the above code:

All JavaScript objects, including the array object, are actually a hash table, and the property name is the key of the hash table, and the value of the property is the value of the hash table.

The array object is not related to the usual meaning of an array object, and the usual array can only be positioned by subscript, while the array in JavaScript can locate objects in the collection like a hash table by using a key.

You can assign a function directly to the object's hash table as a value.

Front-end time look at Nanyi's data type and JSON format, which refers to dividing all the data into three types when Yaml describes the data:

The first type is scalar (scalar), a separate string or number, such as "Beijing", a separate word.

The second type is sequence (sequence), with several related data tied together in a certain order, also known as array (array) or list, such as "Beijing, Tokyo".

The third type is map (map), a key/value pair (Key/value), also known as hash (hash) or dictionary (dictionary), such as "Capital: Beijing."

Maybe we're all familiar with these three types, but the four rules for JSON that are mentioned in this article just parse the way JavaScript describes the data:

The data is separated by commas (",").

Mappings are represented by a colon (":").

The collection of parallel data (arrays) is represented by square brackets ("[]").

The mapped collection (object) is represented by braces ("{}").

With these four rules (plus the understanding of function), many of the words that look very "weird" can be understood. So, a JavaScript object is actually an array or a map.
With regard to the differences between arrays and mappings, you can look at the following example:

The code is as follows Copy Code
var m = {
Name: "Keel",
Age:5
}
var a = [M, "SSS", 3];
The following request successfully navigates to the Name property
Alert (m["name"]);
Alert (a[0]["name"]);//a[0] navigate to M
alert (a[0].name);
Following failure
Alert (m[0]);//mapping cannot be accessed as an array subscript

From the point of view of the member, the mapping uses the key to locate the members, and the array uses subscript, the map can not use subscript positioning, the same array can not use the key (of course, there is no key);

From the perspective of presentation, mappings can be accessed using similar object properties (such as: M.name) or with a key (such as: m["name"), which is a special case of JavaScript that looks like an array and is actually still mapped), but the array can only use subscript;

From the order point of view: The array is sequential, mapping is unordered;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.