我有一种感觉,这可能是不可能的,但我想确定一个已经传递给javascript中的函数的变量的原始变量名。我不知道如何更好地解释它,所以看看这个例子是否有意义。
function getVariableName(unknownVariable){
return unknownVariable.originalName;
}
getVariableName(foo); //returns string "foo";
getVariableName(bar); //returns string "bar";
这是我正在处理的jquery插件,我希望能够显示传递给“debug”函数的变量名称。
答案 0 :(得分:19)
你是对的,这在任何理智的方式都是不可能的,因为只有值被传递到函数中。
答案 1 :(得分:3)
这要归功于ES6:
function getVariableName(unknownVariableInAHash){
return Object.keys(unknownVariableInAHash)[0]
}
const foo = 42
const bar = 'baz'
console.log(getVariableName({foo})) //returns string "foo"
console.log(getVariableName({bar})) //returns string "bar"
唯一(小)的问题是你必须在{}
之间包装你的未知变量,这没什么大不了的。
答案 2 :(得分:1)
这是一种可用于保存变量名称和值的技术。
// Set up a global variable called g
var g = {};
// All other variables should be defined as properties of this global object
g.foo = 'hello';
g.bar = 'world';
// Setup function
function doStuff(str) {
if (str in g) {
var name = str;
var value = g[str];
// Do stuff with the variable name and the variable value here
// For this example, simply print to console
console.log(name, value);
} else {
console.error('Oh snap! That variable does not exist!');
}
}
// Call the function
doStuff('foo'); // log: foo hello
doStuff('bar'); // log: bar world
doStuff('fakeVariable'); // error: Oh snap! That variable does not exist!
这有效地创建了一个将变量名称映射到其值的字典。如果不重构每个变量,这可能不适用于现有代码。但是使用这种风格,您可以为这类问题找到解决方案。
在ES6 / ES2015中,您可以initialize一个具有名称和价值的对象,几乎可以实现您的目标。
function getVariableName(unknownVariable) {
return Object.keys(unknownVariable)[0];
}
var foo = 'hello';
var output = getVariableName({ foo }); // Note the curly brackets
console.log(output);
这是有效的,因为您使用键foo
创建了一个新对象,其值与变量foo相同,在本例中为hello
。然后我们的helper方法将第一个键作为字符串。
信用转到this tweet。
答案 3 :(得分:1)
那么,所有全局变量都是全局对象(this或window)的属性,它们不是吗? 因此,当我想找出变量的名称时,我做了以下功能:
var getName = function(variable) {
for (var prop in window) {
if (variable === window[prop]) {
return prop;
}
}
}
var helloWorld = "Hello World!";
console.log(getName(helloWorld)); // "helloWorld"
有时不起作用,例如,如果在没有new运算符的情况下创建2个字符串并且具有相同的值。
答案 4 :(得分:0)
将一组唯一变量转换为一个我编写此函数的JSON对象
function makeJSON(){ //Pass the variable names as string parameters [not by reference]
ret={};
for(i=0; i<arguments.length; i++){
eval("ret."+arguments[i]+"="+arguments[i]);
}
return ret;
}
示例:
a=b=c=3;
console.log(makeJSON('a','b','c'));
也许这就是这个查询的原因
答案 5 :(得分:0)
要调试时(显示var的名称和var的值), 我也一直在寻找它,只想分享我的发现。
这不是从var中检索var的名称,而是从var的名称(作为字符串)中检索var的值。
可以在不使用eval且使用非常简单的代码的情况下进行操作,条件是将var传递给函数并用引号引起来,然后全局声明变量:
Pwd=yyy
答案 6 :(得分:0)
我认为您可以使用
getVariableName({foo});
答案 7 :(得分:0)
注意:我现在觉得上面@Offermo 的答案是最好的。留下我的答案以供参考,尽管我通常不建议使用它。
这是我独立提出的,它需要显式声明变量名称并且仅适用于唯一值。 (但如果满足这两个条件就可以了。)
// Initialize some variables
let var1 = "stick"
let var2 = "goo"
let var3 = "hello"
let var4 = "asdf"
// Create a 2D array of variable names
const varNames = [
[var1, "var1"],
[var2, "var2"],
[var3, "var3"]
]
// Return either name of variable or `undefined` if no match
const getName = v => varNames.filter(name => name[0] === v).length
? varNames.filter(name => name[0] === v)[0][1]
: undefined
// Use `getName` with OP's original function
function getVariableName(unknownVariable){
return getName(unknownVariable)
}