JavaScript----Hash-based object-oriented

Source: Internet
Author: User

Structure of the 1.Hash table

First, allow us to take a moment to introduce the hash table briefly.

1. What is a hash table

A hash table is a two-dimensional structure. Attach a picture and we'll start a lengthy speech.

As shown, the left side is an array of one-dimensional sequential storage, and the array stores pointers to another chained array. The green part is a pair of pairs of <key,value> the white part on the right side of the green section is a pointer to a pair of key values down.

How the hash table works:

(1). The hash value is obtained according to the key and hash algorithm, which is the corresponding array subscript.

(2). According to the array subscript to get the pointer stored in this subscript, if the pointer is empty, there is no such a key value pair, otherwise according to this pointer to get this chain array. (This chain array contains a pair of pairs of <Key,Value>).

(3). Traverse this chain array, if you find a key equal to the given key, that is, this hash table has this <Key,Value> key value pair, then you can operate on this key value pair, if not found, that is, there is no such key value pair.

So the hash table is actually the management of a pair of <Key,Value> such structure.

2. The inevitable hash conflict

The hash table manages the <key,value> of a group of groups;. Take a hash of the key. Hashing is based on a hashing algorithm, ideally the same key hash is the same value, the different key hash column is different values. In reality, however, because of the limited storage space, this algorithm is impossible to implement, so when the different keys are scattered as the same value, there is a conflict. This is what we call the hash conflict.

Let's take a look at the ideal case of a one-dimensional hash table: for example:

(2) Ideal case of a one-dimensional hash table

Ideally, the hash table holds a pair of pairs of <key,Value> key values,

1. Hash hashing algorithm requirements: Different keys must hash different values, the same key must be scattered as the same value.

2. For the requirements of an array, to maintain the efficiency of the lookup, it must be kept in order to store the arrays.

Ok.... So the problem is, the ideal hash table can solve the following problems perfectly:

(1). In order to be efficient, you are an array of sequential storage, then do you know how much storage you need to open up to this array each time? If too big, will inevitably cause the waste of space, and this space must also be continuous, if too small, need to constantly adjust. So you can still stay productive?

(2). For each <Key,Value> how much space do you prepare to store this key-value pair? I'm sorry to tell you ... You don't know. Too larger than the small will face the question we asked just now?

When the problem arises, how many geniuses are there to emerge?

The perfect implementation of the 3.hash table (allows for the existence of conflicts)

Hash lookup is an efficient lookup, and with the compromise between storage space and lookup time, someone proposes a new idea that allows different key hashes to be the same value.

The specific implementation is as follows:

1. First open an array in space. We first get the subscript of the array based on the key and hash algorithm, which is the partial left array.

2. Each cell in the array maintains a chain array, and the chain array holds <key,value>

3. When accessing, according to the key one by one comparison, if found, obtained value, if not found, different languages return different values.

4. When storing, first find out if this Key exists, if present, modify this value, if not, then the end of this chain array plus a pair of <Key,Value>;

With the above we can see that the use of hash table storage objects, the time to query the speed is very fast. The left array can be accessed randomly, while the right chain array needs to be traversed, but if the hashing algorithm is good enough, the number of key-value pairs stored will be small. The search speed is also fast enough. In the ideal case, the search speed is O (1).

What is the storage format for objects in 2.JacaScript?

1. Two different types of objects to create

(1). creating an object with a constructor function

1 var object=New Object (); 2 object.x=1; 3 object.2=1; Error because the variable cannot begin with a number:

The above mistakes believe that we all know, then use the literal creation of objects?

(2). create an object with literal amounts

var object = {  1,  2:2};//does not error

(3) Two ways of comparison

Do you think it's strange that we should compare the two? ------Why the first way of error, the second is not?

Of course, you can also understand that the JS engine is blocking syntax errors. So why does the JS engine bother to shield the offending variables so much? The answer is: there's a reason for---.

Suppose JS allows you to assign values in this way or access

object.2=2; Assuming there is no error
2==2;
Console.log (2);//How do you make the JS engine happen when this happens? is to understand 2 as a global variable or a number 2

When this happens, the interpreter can only be despair. Therefore, in order to unify the grammar, we define various requirements for the variable-type Access object variables.

In other words, the JS engine is reasonable for shielding against illegal variables.

So the question comes again----

Since there is a requirement for the form of a variable, why do you allow this kind of thing to exist? -----answer is: because it is also reasonable/
var object = {   2:2};

Why is this way reasonable?

Because there is no such or such requirement for key in the <Key,Value> key pair of the hash table. So, if the JS object is based on a hash table to store variables, since this form is valid at the time of storage, then why don't you allow me to access my stored variables? This is not a contradiction!

Of course, there is a logical question, what do I think the storage of JS object is based on hash? (I'll explain this in part III)

The above lists two methods of defining object variables and the differences, and then explains why the two definition forms clearly conflict with each other but also exist!

Let's start with our discussion: What is the key in the hash table of JS object? ------Answer: String

First, let's take a look at his output.

var object = {  1,  2:2} for (var in object) {  Console.log (typeof Property + ') ' + property + ': ' + object[property]);}
Result  (string) 2:2
     (String) x:1

The above code proves two points: object[2] is really there. And the Key in <Key,Value> is in the form of a string. In other words, the object's variables and values are stored as key-value pairs in the hash table, and key is in the form of a string? So what is value in the form of existence? We'll discuss this later. It is also worth discussing.

Summarize:

When the object is initialized with literals, the variable name can make the number, or it can be a string, but it can not be an object, because if there is an object, it will result in semantic ambiguity, although the hash storage structure allowed, but in the JS semantic analysis is not legal, but in the future with [] access, the contents of [] It can be an object.

Both forms of defining a variable are legal, but access to the. Number only accesses the legal variables we normally call, while accessing it is more free and less demanding.

As shown below:

var object={    {x:1}:2   ///This form is wrong because the interpreter does not correctly recognize this syntax and will error. }


The following methods will not be error-
var object={};
Object[{x:1}]=1;
Console.log (Object[{x:1}]); the//1 interpreter will do its best to interpret the contents of [] as strings.

Know that key is a character creation then we can now explain the problem: object[2] and object["2"] there is a difference? -----(Of course, it depends on how you understand)! To prove this, we're looking at two examples.

Consider: What does the interpreter do for us when we use [] to access the object variable?

var object = {  1,  2:2}console.log (object[2]);//2console.log ( object["2"]),//2object[2]=3console.log (object["2"]);//3

The reason object[2] and object["2" are equivalent, that's because the interpreter helped us do a little work, so what did the interpreter do for us?

When the interpreter accesses Object[2], it first converts the 2 inside the square brackets into a string. Then visit, in order to prove this, I wrote a little code to prove it.

var object = {  1,  2:2function  () {  return  ' 2 '1}]); 2
Console.log (object["2"]); 2
Console.log (object[2]); 2

Take a moment to analyze the execution of the above code:

1. First defines an object object with two variables.

2. Override the ToString () method in the object prototype.

3. In line 7th, we used a temporary variable {x:1} for []. When this variable is initialized, in object[] execution, the parsing of {x:1} in square brackets is an object, at which time the interpreter converts the object to a string, and if the reference type invokes the ToString () function in the prototype object, If the base data type is also the base data type is converted to the corresponding string, the result is access to object["2"]. The result of the output is 2.

So we also indirectly prove that the JS object, all the key is a string, even if you access the time is not the form of a string, the interpreter will try to convert it to a string first.

So the following two ways are completely equivalent:

1 var object1 = {2   x:1,3   1:24}//First type 5  var object2 = {6   ' x ': 1,7   ' 1 ': 28 }  //second type

Above we discussed is the JS object storage form, how the data is stored.

Let's discuss why the object in JS is based on hash.

the objects in 3.JavaScript are hash-based.

(1). Proof:

We can add or remove variables to an object at any time (if this variable allows deletion)

1 var object={}; 2 object.x=1;//Add a variable delete  object.x;//Delete a variable success4 Console.log (Object.keys (object). length); 0

(1) Since the object type and number of variables are variable, we cannot assign an object to a fixed space like java,c++. The space occupied by the object that the JS reference refers to must support adjustment at any time. Based on this, the sequentially stored array has been eliminated.

(2) The disadvantage of the slow-link array query is that it cannot be used as the storage structure of variables in the object.

(3) Of course you can say that I can use the tree storage structure, the more efficient may be the balance tree. The time complexity of the balance tree is log (n), not too high, but when the attribute is deleted, the balance tree is much more expensive to adjust than the hash table. Perhaps, you have other options, but I dare say that there is no such a hash table to store data so convenient and efficient.

(4). Only JavaScript objects are stored based on the hash table, so all of the unreasonable things that exist in c++,java,c# will become reasonable.

In fact, white, physical storage is not a non-hash table, but there is no better than the hash table, so in the JS language design is as a hash table structure design.

To prove that the object is stored in key-value pairs based on the hash table, let's take a simple look at the arrays and functions:

Function:

var person=function  y () {};p erson.x=1;p erson.y=2;  for (var in person ) {  Console.log (property + ":" + Person[property])}//x:1
Y:2

Array:

var array=[1,2,4]for (var in array) {  Console.log + ":" + Array[property])}//0:1
1:2
2:4

(2). Understand what this is for:

1. You also can have the array number of properties, functions can also have properties strange? Because they manage their own hash tables, they can add or remove properties at any time.

2. We know that the following table in the array can be arbitrarily added, no matter how big you set, you can also cross-border access (strictly speaking, there is no so-called cross-border), but the result of the return is undefined ... Because the corresponding key was not found.

You understand it as subscript, rather than as a key, so, JS array is so mysterious? Plainly, it is a hash structure. If you want to, you can also think of object as an array, and then customize a whole set of functions, but it may not be so convenient.

Note: Of course, the function as an object must have its particularity. We don't have much discussion here.

4.JS object is a typical application of hash table

Array de-weight

1 varArray=[' true ',true,false, ' 1 ', 1, ', ' sss ', ' ", 1,34,,, {x:1},{x:2}]2 3Array.prototype.unique=function(){4 5Using the object's hash storage feature to remove weight6   varobject={},result=[]; 7     8    for(varI=0,length= This. length;i<length;i++){9     Ten     vartemp= This[I],key;  One         A     if((typeoftemp) = = ' object '){         -key=json.stringify (temp);//If you are an object type, serialize the object to a string -}Else{ thekey=typeoftemp+temp;  -     } -      -     if(!Object[key]) {     +object[key]=true; If this key value already exists in object, it proves that the element already exists in the array - Result.push (temp); +      A     }     at      -   } -    -   returnresult; -  - } in  -Console.log (Array.unique ());
//["True",True,False,"1",1,"" "SSS" 34< Span class= "Arraycomma", undefinedobject {x=1} object {x< Span class= "objectequal" >=2" ]

The disadvantage of this algorithm is that, because of the addition of an object object and a result array, the comparison takes up space but is very fast. This is the improvement of some algorithms on the net, there are a lot of algorithms for object hash on the network is not perfect to go heavy.

For example, array [1, "1", {x:1},{x:2}].

The doubts that exist in the article: what is the method that the basic types of number,boolean are called when they are converted to strings? (Not the ToString () method in the prototype chain, which is not described, welcome to add);

JavaScript----Hash-based object-oriented

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.