1
방금 "Define global variable in a JavaScript function"을 읽었으므로 동일한 작업을 수행하려고했지만 이번에는 문자열을 전역 변수 이름으로 전달했습니다.이름을 문자열로 전달하여 전역 변수를 만들려면 어떻게해야합니까?
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
내가 객체에 정말 새로운 오전, 나는 또한 결과없이 window.[strname]
, window.[""+strname+""]
및 window.["'"+strname+"'"]
같은 이상한 일을했습니다.
이름을 문자열로 전달하여 전역 변수를 만들려면 어떻게해야합니까?
내가 너무 가까이, 감사 – ajax333221