在Javascript中获取变量的“名称” [英] Get the 'name' of a variable in Javascript

查看:105
本文介绍了在Javascript中获取变量的“名称”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

确定变量传递给函数后的原始名称。

我想知道是否可以获得变量的实际名称。

I would like to know if its possible to get the actual name of a variable.

例如:

var foo = 'bar';
function getName(myvar) {  
  //some code
  return "foo"  
};  

因此getName(foo)将返回foo

So for getName(foo) will return "foo"

这可能吗?

谢谢。

推荐答案

我认为不可能。调用函数时,传递对象而不是变量。该功能并不关心对象来自何处。

I don't think it is possible. When you call a function you pass an object, not a variable. The function doesn't care where the object came from.

如果按以下方式调用函数,可以采用其他方式:

You can go the other way though if you call your function as follows:

getName('foo') 

或传递价值和名称:

getName(foo, 'foo') 

这篇关于在Javascript中获取变量的“名称”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆