Home  >  Article  >  Web Front-end  >  JavaScript objects, arrays, and hash tables

JavaScript objects, arrays, and hash tables

高洛峰
高洛峰Original
2016-11-28 15:13:331786browse

In JavaScript, an object is actually a hash table, such as the following user object:

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

Slightly change, use Array object instead of 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 way of writing:

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

The above code can It can be seen that:

All JavaScript objects, including Array objects, are actually a hash table. The attribute name is the key of the hash table, and the value of the attribute is the value of the hash table.

Array objects are not related to array objects in the usual sense. Normal arrays can only be located by subscripts, but Array in JavaScript can locate objects in the collection through keys like a hash table.

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

In front-end time, I read Ruan Yifeng’s article "Data Types and Json Format", which mentioned that when describing data in yaml, all data is divided into three types:

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

The second type is sequence. Several related data are arranged together in a certain order, also called array or List, such as "Beijing, Tokyo".

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

We may be familiar with these three types, but the four rules for json mentioned in the article just analyze the way JavaScript describes data:

Parallel data are separated by commas (",").

Mapping is represented by a colon (":").

A collection (array) of parallel data is represented by square brackets ("[]").

Mapped collections (objects) are represented by curly brackets ("{}").

With these four rules (plus an understanding of function), many seemingly "weird" writing methods can be understood. Therefore, a JavaScript object is actually an array or map.

Regarding the difference between arrays and mappings, you can look at the following example:

var m = {
 name:"keel",
 age:5
}
var a = [m,"sss",3];
//以下请求成功定位到name属性
alert(m["name"]);
alert(a[0]["name"]);//a[0]定位到m
alert(a[0].name);
//以下失败
alert(m[0]);//映射无法以数组下标的方式访问

From the perspective of the method of locating members, mapping uses keys to locate members, while arrays use subscripts, and mapping cannot use subscripts to locate members. Similarly Arrays cannot use keys (of course, there is no key at all);

From the perspective of representation, mapping can be accessed using a method similar to object attributes (such as: m.name), or using the [] method with a key ( For example: m["name"], this is a special case of JavaScript, it looks like an array, but is actually a mapping); but arrays can only use subscripts;

From the perspective of order: arrays are in order, mapping It is disordered;


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn