检索变量的名称
Retrieve the NAME of a variable
我想通过我的函数检索变量的名称(不是它的值)。这是我的代码,但它不起作用。为什么?
myVariable = "123";
function myFunction (a) {
varName = window[a]; // to get the NAME !, not the value...
$('body').append(varName);
}
myFunction(myVariable);
// output : undefined
// output expected : "myVariable"
这里的解决方案:(非常感谢@tadman...)
myVariable = "123";
function myFunction (a) {
varName = a;
varValue = window[a];
$('body').append(varName);
$('body').append(varValue);
}
myFunction('myVariable');
// output : 123 myVariable
// parameter "stringify"...!!
我想通过我的函数检索变量的名称(不是它的值)。这是我的代码,但它不起作用。为什么?
myVariable = "123";
function myFunction (a) {
varName = window[a]; // to get the NAME !, not the value...
$('body').append(varName);
}
myFunction(myVariable);
// output : undefined
// output expected : "myVariable"
这里的解决方案:(非常感谢@tadman...)
myVariable = "123";
function myFunction (a) {
varName = a;
varValue = window[a];
$('body').append(varName);
$('body').append(varValue);
}
myFunction('myVariable');
// output : 123 myVariable
// parameter "stringify"...!!