javascript - Set Object name for variable value -


i curious if possible set name of object in javascript variable value. if declare variable objectname , want create new object name value of objectname.

try this:

var objectname = "myobjectname";  window[objectname] = {     foo: "bar" };  console.log(myobjectname); // { foo: 'bar'} 

all global variables properties of window object, , since window object, can create dynamic property using [] syntax. , can access window.myobjectname, window["myobjectname"] or plain myobjectname

more info of window object here: http://www.w3schools.com/js/js_window.asp , here https://developer.mozilla.org/es/docs/web/api/window/window


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -