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
Post a Comment