How to store object as key in a array using javasc

2019-09-02 06:28发布

问题:

Possible Duplicate:
How to store objects in a array using javascript

i want to store object as key in a array what is the solution.

var myTabObj = safari.application.activeBrowserWindow.activeTab;

I want to store myTabObj as key in a array. I think myTabObj is unique for safari browser tab specific.

OR

How to set tab specific values in safari extension development.

I want solution immidiately please help me. for example i have 3 params to set for every tab dynamically what is the solution or any alternative solutions.

Thanks in advance

回答1:

First, you sgould use an object (hash) and not an array. You do that anyway - an array IS an object (hash) in Javascript. You can treat an array as an object (hash) in JS, using non-numeric values as key, but this is messy - you get the Array-functions an dproperties in the objects prototype, e.g. arr.length, but they only work on the numeric keys stored in the array-object.

Example (entered into the JS console of Firebug):

>>> a=[]
[]

>>> a[{}] = 42
42

>>> console.dir(a)      
[object Object]   42
>>> a.length
0
>>> a[{}]
42

First I create an array. Then I create a property with key "{} on which toString was called implicitly" in the object that represents the array. Since this is not a numeric key the array-specific functions ignore it - the array's length is 0. However, as the last line shows, the array-instance, which is an object like everything in JS, DOES store the property.

Second, objects (hashes).

Keys are always strings. That's why some people say you cannot use objects as keys - but that is not true! If you use a non-string value as key in Javascript, the toString() method of that object will be called. EVERY object has that method, because it is defined on Object.prototype.toString. However, without overriding that fallback native method you always get the exact same string for whatever object you have.

Example (entered into the JS console of Firebug):

>>> a={}
Object {}

>>> a[{}] = 1
1

>>> console.dir(a)
[object Object]   1

>>> a[{prop:"text"}] = 1
>>> console.dir(a)
[object Object]   1

So, you CAN use objects as keys, but you have to make sure to have your own toString function defined for all object instances you intent to use. Those toString functions usually print something like a ("class")name plus a (unique) string ID value stored in the instance. If oyur toString functions are not guaranteed to produce a unique string for different objects you cannot use the objects (instances) as keys, well, of course you can but it's obviously a little useless, since you won't have a reliable 1:1 mapping.

This is not concrete help, but I must admit I prefer giving such general advice about how the language works - besides, for giving a concrete solution there is not enough (code) context I'd say. Anyway, to store it in an array simply use

var arr = [];

arr.push(instance);

and then iterate over the array with a for-loop or an "...each()"-function of the JS library framework of your choice, if you use one.



回答2:

You can use my jshashtable to store values using arbitrary objects as keys.