最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to alert variable name, not a value - Stack Overflow

programmeradmin3浏览0评论

How to alert variable name, not a value of variable?

var color = 'red';

alert(color); // Will alert 'red'
alert(/* magic */); // Will alert 'color'

How to alert variable name, not a value of variable?

var color = 'red';

alert(color); // Will alert 'red'
alert(/* magic */); // Will alert 'color'
Share Improve this question asked Apr 17, 2010 at 19:41 Randy GurmentRandy Gurment 1772 silver badges6 bronze badges 1
  • 9 alert("color");? – kennytm Commented Apr 17, 2010 at 19:42
Add a ment  | 

2 Answers 2

Reset to default 5

It's not possible in JavaScript, because arguments in this language are passed by value or by reference, not by name, so when variable is passed to function, its name is lost.

In the Firebug console:

>>> a=[]
[]
>>> a
[]
>>> b=a
[]
>>> a.push(3)
1
>>> b
[3]
>>> a
[3]

So, which variable name would you like that array to return? a? b? Something pletely different?

发布评论

评论列表(0)

  1. 暂无评论