I just read " Define global variable in a JavaScript function " and I wanted to try to do the same, but this time passing a string as the global variable name.
function createGlobal(strname){
window.strname={
name:"John",
age:27
};
}
createGlobal("myglobal");
//can't use "alert(myglobal.name);", myglobal is not defined and crashes
//however, this works v
alert(strname.name); //John
I am really new to objects, I also tried weird things like window.[strname]
, window.[""+strname+""]
and window.["'"+strname+"'"]
without results.
How can I create a global var by passing its name as string?