Heavy duty ToString realizes JS HashMap analysis _javascript Skill

Source: Internet
Author: User
But if you look at it carefully, you'll find that the difference is still great. Java hashmap key is type object, so you can have any type of argument, and JS Key can only be a string or a number. You might say, obj={};map[obj]=1; This code passed in a key that was neither a number nor a character, but it didn't happen. That's because the interpreter converts the Obj object to the character "[Object]" through the built-in ToString method, and you can look at the map below for each. Java is able to accept any type of key because its object implements the Hashcode method, and each class inherits or overrides the object's hashcode, so any variable has a hash value. We can also use JS to try.

The ToString method, used to convert any type into a character, is similar to another method: ValueOf, which is used to transform into numbers. Since numbers are easier to index, we first try valueof:
Copy Code code as follows:

Object.prototype.valueOf = function ()
{
Alert ("hello~")
};

var map = [];
var obj = {};
Map[obj] = 1;

The result was disappointing, and the dialog did not jump out, stating that the JS engine did not attempt to convert the Obj object to a number. Try to modify the ToString method below:
Copy Code code as follows:

Object.prototype.toString = function ()
{
Alert ("hello~")
};

var map = {};
var obj = {};
Map[obj] = 1;

The dialog box jumps out. Of course we did not return the data, this 1 was saved in the map["undefined". But if we return a number and we can guarantee the unique value of each variable, then we can index any type in the most original Map[key] way. We overload the ToString method of object:
Copy Code code as follows:

var hash_id = 0;

Object.prototype.toString = function ()
{
if (This._hash = null)
This._hash = hash_id++;
Return "OBJ:" + this._hash;
};

Here's a test:
Copy Code code as follows:

var HashMap = {};
var obj1 = {};
var obj2 = {};


HASHMAP[OBJ1] = "Foo1";
HASHMAP[OBJ2] = "Foo2";
Alert (Hashmap[obj1] + "&" + Hashmap[obj2]);

HASHMAP[OBJ1] = "BAR1";
HASHMAP[OBJ2] = "BAR2";
Alert (Hashmap[obj1] + "&" + Hashmap[obj2]);

Output separately: Foo1 & Foo2 and Bar1 & Bar2, which shows that obj1,obj2 always corresponds to the same index.

Of course, if object itself overrides the ToString method, it may not necessarily be, and it might return a different value each time. Therefore, the use of the time, according to the actual situation to make corresponding adjustments. (2011/3/12)

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.