Javascript HashTable使用Object键 [英] Javascript HashTable use Object key

查看:100
本文介绍了Javascript HashTable使用Object键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有些事情是这样的:

  var object1 = new Object(); 
var object2 = new Object();

var myHash = new HashTable();

myHash.put(object1,value1);
myHash.put(object2,value2);

alert(myHash.get(object1),myHash.get(object2)); //我希望它能打印出value1值2

编辑: a href =https://stackoverflow.com/a/10908885/1129332>我的答案为完整解决方案

解决方案

以下是一个建议:

 函数HashTable(){
this.hashes = {};


HashTable.prototype = {
构造函数:HashTable,

put:function(key,value){
this.hashes [JSON.stringify(key)] = value;
},

get:function(key){
return this.hashes [JSON.stringify(key)];
}
};

API与您的问题完全相同。



然而,你不能在js中使用引用(所以两个空对象看起来与hashtable相同),因为你无法获得它。有关更多详细信息,请参阅此答案:如何获取JavaScript对象引用或引用计数?



Jsfiddle演示: http:然而,对于事物的独特的一面,你可以使用原始对象,就像这样:

/ p>

 函数HashTable(){
this.hashes = {},
this.id = 0;


HashTable.prototype = {
构造函数:HashTable,

put:function(obj,value){
obj.id = this.id;
this.hashes [this.id] = value;
this.id ++;
},

get:function(obj){
return this.hashes [obj.id];
}
};

Jsfiddle演示: http://jsfiddle.net/HKz3e/2/

这意味着你的对象需要一个名为 id ,你不会在其他地方使用。如果你想让这个属性不可枚举,我建议你看看

这也意味着你在这个散列表中可以存储的项目数量受到限制。限于 2 53 ,就是说。



现在,它不会在任何地方工作解决方案:使用ES6 WeakMaps。它们完全是为了这个目的完成的:将对象当作键。我建议你阅读MDN获取更多信息: https://developer.mozilla.org / en / JavaScript / Reference / Global_Objects / WeakMap



它与你的API略有不同(它是 set $ b $ pre $ var myMap = new WeakMap( ),
object1 = {},
object2 = {};

myMap.set(object1,'value1');
myMap.set(object2,'value2');

console.log(myMap.get(object1)); //value1
console.log(myMap.get(object2)); Jsfiddle使用weakmap shim演示:

然而,在FF和Chrome中实现了weakmaps(如果启用chrome中的Experimental javascript features标志,则仅适用于 )。有垫片可用,像这样: https://gist.github.com/1269991 。您可以使用地图,它们可能更符合您的需求,因为您也可以需要将原始值(字符串)存储为键。 文档 Shim


I want to create a hash table that gets my Object as his key without converting it to String.

Some thing like this:

var object1 = new Object();
var object2 = new Object();

var myHash = new HashTable();

myHash.put(object1, "value1");
myHash.put(object2, "value2");

alert(myHash.get(object1), myHash.get(object2)); // I wish that it will print value1 value2

EDIT: See my answer for full solution

解决方案

Here is a proposal:

function HashTable() {
    this.hashes = {};
}

HashTable.prototype = {
    constructor: HashTable,

    put: function( key, value ) {
        this.hashes[ JSON.stringify( key ) ] = value;
    },

    get: function( key ) {
        return this.hashes[ JSON.stringify( key ) ];
    }
};

The API is exactly as shown in your question.

You can't play with the reference in js however (so two empty objects will look like the same to the hashtable), because you have no way to get it. See this answer for more details: How to get javascript object references or reference count?

Jsfiddle demo: http://jsfiddle.net/HKz3e/

However, for the unique side of things, you could play with the original objects, like in this way:

function HashTable() {
    this.hashes = {},
    this.id = 0;
}

HashTable.prototype = {
    constructor: HashTable,

    put: function( obj, value ) {
        obj.id = this.id;
        this.hashes[ this.id ] = value;
        this.id++;
    },

    get: function( obj ) {
        return this.hashes[ obj.id ];
    }
};

Jsfiddle demo: http://jsfiddle.net/HKz3e/2/

This means that your objects need to have a property named id that you won't use elsewhere. If you want to have this property as non-enumerable, I suggest you take a look at defineProperty (it's not cross-browser however, even with ES5-Shim, it doesn't work in IE7).

It also means you are limited on the number of items you can store in this hashtable. Limited to 253, that is.

And now, the "it's not going to work anywhere" solution: use ES6 WeakMaps. They are done exactly for this purpose: having objects as keys. I suggest you read MDN for more information: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/WeakMap

It slightly differs from your API though (it's set and not put):

var myMap = new WeakMap(),
    object1 = {},
    object2 = {};

myMap.set( object1, 'value1' );
myMap.set( object2, 'value2' );

console.log( myMap.get( object1 ) ); // "value1"
console.log( myMap.get( object2 ) ); // "value2"

Jsfiddle demo with a weakmap shim: http://jsfiddle.net/Ralt/HKz3e/9/

However, weakmaps are implemented in FF and Chrome (only if you enable the "Experimental javascript features" flag in chrome however). There are shims available, like this one: https://gist.github.com/1269991. Use at your own risk.

You can also use Maps, they may more suit your needs, since you also need to store primitive values (strings) as keys. Doc, Shim.

这篇关于Javascript HashTable使用Object键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆