本文介绍了如何获取变量的名称(不是值) - 内省????的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 Hello All, 我正在试图弄清楚如何从代码中获取变量的名称。 Ex : var MYVAR = 3; alert(?????); 输出:" ; MYVAR" 搜索: javascript内省 和 "变量名称" javascript 没有在网上找到任何帮助 - 有谁知道这个?..是否有可能在目前的规格 语言??? 谢谢 - Eric 解决方案 为什么? 严格来说,ECMAScript实现中的变量有一个 标识符,而不是名称。 你的Shift键出现故障。 (您的问号键也无法正常工作。) 否,它不会可以在任何未来的实施中使用 规范。变量和 值之间根本没有1:1的关系,因为任何变量都可能有任何价值。 但ISTM你的意思是 window.alert(" MYVAR =" + MYVAR); 你可以写 function alertValue(s) { window.alert(s +" =" + eval(s)); } //示例 alertValue(" MYVAR"); 为一般解决方案。 PointedEars - var bugRiddenCrashPronePieceOfJunk =( navigator.userAgent.indexOf(''MSIE 5'')! = -1 && navigator.userAgent.indexOf(''Mac'')!= -1 )// Plone,register_function.js:16 < snip> 编写var MYVAR时,你会告诉编译器两件事。 首先,你要告诉它创建一个变量,即创造空间能够持有价值的b $ b。其次,你告诉它当你写''MYVAR'' 你的意思是变量。 你想指出什么并找出你给它的名字 是''MYVAR''。但是如何在不说''MYVAR'的情况下指出它? 一种方法是单独记住它: var MYVAR = 3; vars [14] =''MYVAR''; 现在你必须记住两件事,''vars''和14,所以你的情况会更糟。 也许你可以得到源代码并搜索声明。 现在你必须记住要搜索的内容,所以再次你会更糟关闭。 也许有更好的方法。你真的想要实现什么? John - John Harris < snip> 编写var MYVAR时,你会告诉编译器两件事。 首先,你要告诉它创建一个变量,即创造空间能够持有价值的b $ b。其次,你告诉它当你写''MYVAR'' 你的意思是变量。 你想指出什么并找出你给它的名字 是''MYVAR''。但是如何在不说''MYVAR'的情况下指出它? 一种方法是单独记住它: var MYVAR = 3; vars [14] =''MYVAR''; 现在你必须记住两件事,''vars''和14,所以你的情况会更糟。 也许你可以得到源代码并搜索声明。 现在你必须记住要搜索的内容,所以再次你会更糟关闭。 也许有更好的方法。你真的想要实现什么? 我只是对这个几乎常见的关于获得字面值 值的常见问题很好奇。有些人在这里询问它绝对不是新手编程,至少基于他们在语言中的语言'' 描述和代码示例。那么为什么总是这么大的惊喜哦, 这是不可能的?!每次?是否有一些很大程度上使用/学习的 语言,这是一个琐事?这是什么语言?它是如何实现这样的机制来说多个参考? Hello All,I am trying to figure out a how to get a variable''s name from code.Ex:var MYVAR = 3;alert( ????? );OUTPUT: "MYVAR"Searched under:javascript introspectionand"variable name" javascriptNOT FINDING ANY HELP ON THE WEB- anyone know this?.. is it possible in the current spec of thelanguage???Thanks - Eric 解决方案Why?Strictly speaking, a variable in ECMAScript implementations has anidentifier, not a name.Your Shift key is malfunctioning.(Your Question Mark key doesn''t work properly either.)No, and it is not going to be possible in any implementation of a futureSpecification. There simply is no 1:1 relationship between variables andvalues as any variable may have any value.But ISTM you meant something likewindow.alert("MYVAR = " + MYVAR);and you can writefunction alertValue(s){window.alert(s + " = " + eval(s));}// examplealertValue("MYVAR");for a general solution.PointedEars--var bugRiddenCrashPronePieceOfJunk = (navigator.userAgent.indexOf(''MSIE 5'') != -1&& navigator.userAgent.indexOf(''Mac'') != -1) // Plone, register_function.js:16<snip>When you write var MYVAR you are telling the compiler two things.First, you are telling it to create a variable, i.e to create space ableto hold a value. Second, you are telling it that when you write ''MYVAR''you mean that variable.You want to point to something and find out that the name you gave to itwas ''MYVAR''. But how do you point to it without saying ''MYVAR'' ?One way is to remember it separately :var MYVAR = 3;vars[14] = ''MYVAR'';Now you have to remember two things, ''vars'' and 14, so you''re worse off.Perhaps you could get at the source code and search for the declaration.Now you have to remember what to search for, so again you''re worse off.Perhaps there''s a better way. What do you really want to achieve ?John--John Harris <snip>When you write var MYVAR you are telling the compiler two things.First, you are telling it to create a variable, i.e to create space ableto hold a value. Second, you are telling it that when you write ''MYVAR''you mean that variable.You want to point to something and find out that the name you gave to itwas ''MYVAR''. But how do you point to it without saying ''MYVAR'' ?One way is to remember it separately : var MYVAR = 3; vars[14] = ''MYVAR'';Now you have to remember two things, ''vars'' and 14, so you''re worse off.Perhaps you could get at the source code and search for the declaration.Now you have to remember what to search for, so again you''re worse off.Perhaps there''s a better way. What do you really want to achieve ?I am just curious about this nearly FAQ about getting the literalvalues. Some people asking about it here are definitely not novices inthe programming, at least based on their language in problems''description and code samples. So why always such a huge surprise "oh,it is not possible?!" every time? Is there some largely used/learnedlanguage where it is a trivia? What language is that and how does itimplement such mechanics for say multiple references? 这篇关于如何获取变量的名称(不是值) - 内省????的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-12 19:16