-1

how would I make an object in javascript with a computed name.

Context: I am making an add-on that will sit in the browser, log each hostname visited, create an object named after each one. I'm replacing "." with "_" So for example on this site it would create a stackoverflow_com object.

is this possible?

another example would be var 1+1 (with the variable actually being 2)

I know brackets make this possible with properies but I don't know how to do it with the name itself.

1 Answers1

2

You need to store them either in a global object (like window) or preferably an object of your choosing

var mySites = {};
mySites["stackoverflow_com"] = "foo"; // access as mySites.stackoverflow_com or mySites["stackoverflow_com"]
mySites[1+1] = "bar"; // access as mySites[2];

More info: JavaScript property access: dot notation vs. brackets?

Community
  • 1
  • 1
Jamiec
  • 133,658
  • 13
  • 134
  • 193