Further understanding of JavaScript objects, arrays, mappings, and hash tables

Source: Internet
Author: User
All JavaScript objects, including array objects, are actually a hash table, The attribute name is the key of the hash table, and the attribute value is the value of the hash table.. An array object is irrelevant to an array object of the common meaning. A common array can only be located by subscript.

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]);}

With a slight modification, the array object is used to replace the object:

var user = new Array();user['name']='tom';user['age']=18;user['toString']=function(){return 'Name:' + this.name + ', Age:' + this.age;}alert(user.toString());

Simpler Syntax:

var u = {'name':'tom','age':18}u.toString = function(){return 'Name:' + this.name + ', Age:' + this.age;};alert(u.toString());

The code above shows that:

  • All JavaScript objects, including array objects, are actually a hash table. The attribute name is the key of the hash table, and the attribute value is the value of the hash table.
  • An array object is irrelevant to an array object of the common meaning. An array can only be located by subscript, while an array in javascript can locate objects in a set by key like a hash table.
  • You can assign a function directly as a value to the "hash table" of the object ".

At the front-end time, I read Ruan Yifeng's article "data type and JSON format". I mentioned that when yaml describes data, it divides all the data into three types:

  • The first type is scalar (scalar), a separate string or number, such as the word "Beijing.
  • The second type is sequence. Several related data are grouped together in a certain order, which is also called array or list, such as "Beijing, Tokyo ".
  • The third type is map, a key/value pair (key/value), also known as hash (hash) or dictionary (dictionary), such as "Capital: Beijing ".

We may be familiar with these three types, but the four JSON rules mentioned in this article only analyze the Javascript data description method:

  • Separated by commas.
  • The ing is represented by a colon.
  • The Set (array) of the parallel data is represented by square brackets.
  • The ing set (object) is represented by braces.

With these four rules (coupled with the understanding of Functions), you can understand many seemingly "weird" statements. Therefore, a JavaScript Object is actually an array or ing.
For differences between arrays and mappings, see the following example:

VaR M = {Name: 'keel', age: 5} var A = [M, 'ss', 3]; // The following request is successfully located at the name attribute alert (M ['name']); alert (A [0] ['name']); // A [0] locates malert (A [0]. name); // The following failed alert (M [0]); // ing cannot be accessed as an array subject
  • From the method of locating members, the key ing uses keys to locate the members, while the array uses subscript, And the ing cannot use subscript to locate the members. Similarly, the array cannot use the key (of course, there is no key at all );
  • From the perspective of the representation method, the ing can be accessed in a way similar to the object property (for example, M. name). You can also use the [] method with the key (for example, m ['name']. This is a special case of JavaScript. It looks like an array and is actually still mapped ); however, the array can only use the method of the lower mark;
  • In order: arrays are ordered, and mappings are unordered;

From: http://ipmtea.net/javascript/201007/16_128.html

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.