Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I want to make a map object that key is object.

But, javascript only allows string as a hash key. (at least before ECMAScript6 comes?)

So, I tried to write auxiliary functions to emulate, hash-ish (but actually linear search). Is there any suggestion to improve ?

var tabInfo = {};
var tabInfoKey = 0;

function getTabInfoItem(tab) {
  console.log("getTabInfoItem", tab);
  for(var key in tabInfo) {
    if(tabInfo[key].tab === tab) {
      return tabInfo[key].info;
    }
  }
}

function setTabInfoItem(tab, info) {
  console.log("setTabInfoItem", tab, info);
  for(var key in tabInfo) {
    if(tabInfo[key].tab === tab) {
      tabInfo[key] = {
        tab: tab,
        info: info
      };
      return;
    }
  }
  tabInfoKey++;
  tabInfo[tabInfoKey] = {
    tab: tab,
    info: info
  };
}

function deleteTabInfoItem(tab) {
  console.log("deleteInfoItem", tab);
  for(var key in tabInfo) {
    if(tabInfo[key].tab === tab) {
      delete tabInfo[key];
      return;
    }
  }
}
share|improve this question
3  
Why don't you simply use a shim? – Florian Margaine May 6 at 12:02
Thanks, I'll check the library. – Tsuneo Yoshioka May 6 at 19:30

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.