[英]Can I convert the argument of a function into its own variable's name?
So I want to have something like this: 所以我想要这样的东西:
function trace(variable){
console.log(variables_name.toString()+":"+variable);
}
Is this possible, and if so, how can I invoke/print the variable's name? 这可能吗?如果可以,如何调用/打印变量名?
This way I can succinctly write trace(a_variable)
and have a good idea of what it is without having to write console.log("variable name: "+ variable);
这样,我可以简洁地编写trace(a_variable)
,而不必编写console.log("variable name: "+ variable);
You could pass a map: 您可以通过地图:
function trace(variableMap){
for (var key in variableMap){
console.log(key+":"+variableMap[key]);
}
}
trace ({someVar : someVar});
Since a mod accepted this answer for OP I'll go ahead and add that simply doing 由于国防部接受了OP的答案,因此我将继续添加
trace({someVar});
Also seems to work. 似乎也可行。 This notation depends on ECMAScript 6 is called the Object Literal Property Value Shorthand . 此表示法取决于ECMAScript 6,称为“ 对象文字属性值速记” 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.