Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement JS HashMap by overloading toString

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Reload toString how to achieve JS HashMap, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Anyone who has used Java knows that there is a powerful data structure-HashMap, which provides key-to-value access. However, friends who are familiar with JS will also say that JS is full of hashmap, because each object provides the access form of map.

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

I mentioned the toString method, which is used to convert any type to characters; similarly, there is another method: valueOf, which is used to convert to numbers. Because numbers are easier to index, let's try valueOf:

Object.prototype.valueOf = function () {alert ("Hello~")}; var map = []; var obj = {}; map [obj] = 1

The result was disappointed that the dialog box did not pop up, indicating that the JS engine did not try to convert obj objects to numbers. Try to change it to the toString method again:

View source print?

Object.prototype.toString = function () {alert ("Hello~")}; var map = {}; var obj = {}; map [obj] = 1

Then the dialog box pops up. Of course, we didn't return the data, so the 1 is saved in map ["undefined"]. But if we return a value and guarantee the value of each variable, we can index any type in the original map [key] way. We overload the toString method of Object:

Var HASH_ID = 0; Object.prototype.toString = function () {if (this. _ HASH = = null) this. _ HASH = HASH_ID++; return "Obj:" + this. _ HASH;}

Here's a test:

View source print?

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: Foo1 & Foo2 and Bar1 & Bar2 respectively, which shows that obj1,obj2 always corresponds to the same index.

Of course, not necessarily if object itself overrides the toString method, which may return a different value each time. Therefore, when using it, we should make corresponding adjustments according to the actual situation.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report