Is there an easy way to create dynamic variables with Javascript?

后端 未结 5 454
你的背包
你的背包 2020-12-01 07:06

I\'ve built a data-driven google map with different icons that get assigned to the map depending on the type of item located. So if I have 5 types of landmark, and each gets

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-01 07:37

    It's really easy to do: object["variablename"] = whatever;

    So for example you could have an object: var Landmarks = {} and you could add to it like so: Landmarks["landmark" + i] = new google.maps.Icon(); and pass it that way.

    If you need these variables to be global (why would you?) you can access the global object directly using window.

提交回复
热议问题