Home  >  Article  >  Web Front-end  >  Overloading toString to implement JS HashMap analysis_javascript skills

Overloading toString to implement JS HashMap analysis_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:09:251696browse

But please compare carefully, you will find that the difference is still very big. The key of Java HashMap is of Object type, so any type of parameter can be used, while the key of JS can only be a string or a number. You may say, obj={};map[obj]=1; This code passes in a key that is neither a number nor a character, but no error occurs. That's because the interpreter converts the obj object into the character "[object Object]" through the built-in toString method. You can use for each to map it. The reason why Java can accept any type of key is because its Object implements the HashCode method, and each class inherits or overrides the HashCode of Object, so any variable has a hash value. We can also try it with JS.

The toString method mentioned earlier is used to convert any type into characters; similar to it, there is another method: valueOf, used to convert into numbers. Because numbers are easier to index, let’s try valueOf first:

Copy code The code is as follows:

Object .prototype.valueOf = function()
{
alert("Hello~")
};

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

The result is very disappointing, the dialog box does not pop up, indicating that the JS engine did not try to convert the obj object into a number. Now try changing it to the toString method:
Copy the code The code is as follows:

Object.prototype. toString = function()
{
alert("Hello~")
};

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

The dialog box pops up. Of course we did not return data, this 1 was stored in map["undefined"]. But if we return a value and can guarantee a unique value for each variable, then we can use the original map[key] method to index any type. We overload the toString method of Object:
Copy code The code is as follows:

var HASH_ID = 0 ;

Object.prototype.toString = function()
{
if(this._HASH == null)
this._HASH = HASH_ID ;
return "Obj:" this ._HASH;
};

Let’s test it:
Copy code The code is 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 respectively: Foo1 & Foo2 and Bar1 & Bar2, which illustrates obj1 and obj2 always correspond to the same index.

Of course, if the object itself overrides the toString method, it is not necessarily the case. It may return a different value every time. Therefore, when using it, make corresponding adjustments according to the actual situation. (2011/3/12)
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