简体   繁体   中英

How can i a string use as a objectname in javascript?

How can i use a string as a object name?

var a = "hello",
    b = {
      hello : "word"
    };


alert(b.a);

This give my a in empty box. I want to still use the variable a.

How can ia string use as a objectname in javascript?

This is how you do it.

window["obj"] = { hello:"word"};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM