如何创建名为"命名为#34;的javascript对象动态?
以下是我尝试做的 非工作 示例,问题在于:
"objVarName = "
What does status=canceled for a resource mean in Chrome Developer Tools?
JavaScript [代码]:
function CustomObj(pName, pAge, pColor) {
this.name = pName,
this.age = pAge,
this.color = pColor
}
function createNewObj (objVarName, pName, pAge, pColor){
objVarName = new CustomObj(pName, pAge, pColor);
}
createNewObj("theFirstVarName", "Car", 10, "red" );
alert(theFirstVarName.name); // if working should alert "Car"
答案 0 :(得分:2)
您可以通过更改一行来完成此操作,但我不确定您为什么要这样做:
objVarName = new CustomObj(pName, pAge, pColor);
为:
window[objVarName] = new CustomObj(pName, pAge, pColor);