有没有办法在 JavaScript 中检索变量的名称而不是对象的一部分?这是我要实现的目标的示例:
var obj = {//Declared in a global scope
init:function(){
var var1 = /*value from input*/;
var var2 = /*value from input*/;
doCode({name:/*variable name as a string, "var1"*/,val:var1});
doCode({name:/*variable name as a string, "var2"*/,val:var2});
function checkNumber(args){
if(typeof args.val != 'number')
console.error("Error variable '"+arg.name+"' is not a number!");
else
console.log("Variable '"+arg.name+"' is a number!");
}
}
}
obj.init();