如何在JS中将变量名转换为字符串? [英] How to turn variable name into string in JS?
本文介绍了如何在JS中将变量名转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试为console.log编写一个便利包装,并且我想打印所有传入的变量及其内容。
I am trying to write a convenience wrapper for console.log, and I would like to print any variables passed in along with their contents.
我可以在js中将变量名转换为字符串吗?
Can I turn a variable name into a string in js?
推荐答案
假设您想要这样的东西:
Assuming you want something like this:
function Log(data)
{
console.log(input variable name, data);
}
那么我认为这是不可能的:
Then I don't think it is possible:
为方便起见,您可以做类似
For convenience.. you could do something like
console.log({ "your variable name": your variable});
哪个将输入变为包含确实要记录的变量名的对象。
进行一些输入,但也许会使控制台输出更具可读性。
Which turns the input to an object that does contain the variable name you want to log. A little more typing, but perhaps makes the console output more readable.
这篇关于如何在JS中将变量名转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文