JavaScript 获取变量名
这是我的第一篇文章。
我正在尝试使用 javascript 进行一些基本的元编程,我想知道是否有一种方法可以获取特定对象的 id 并使用该 id 访问变量名称,或者简单地获取某个对象的变量名称特定对象。我想重新创建一种情况,您首先在网页中创建每个 html,然后附加到与特定类(例如类 Person)关联的一些 html 标记事件。例如: 假设下一个代码:
var someFunction = function(someText){alert(someText);}
function SomeClassFunction(){
this.aClassFunction = someFunction;
}
var aVariableName = new SomeClassFunction();
在 HTML 代码中假设我有下一段代码。
<html>
<head>
<title></title>
</head>
<body>
<div onclick="aVariableName.aClassFunction('Some text to show in alert');">
</div>
</body>
</html>
然后,您可能会注意到 onclick 事件使用我之前创建的 aVariableName,但因为我首先创建变量的名称,然后在代码中附加该名称,所以我知道 aVariableName 是该对象的名称。我想要做或实现的是在 html 中创建上面的文本,而不知道特定对象的变量名称。我在网上冲浪,但不幸的是我没有找到任何相关信息。
This is my first post.
I'm trying to do some basic meta-programming with javascript, and I was wondering if there is a way of get the id of a particular object and with that id, access to the variable name, or get simply the variable name of a particular object. I wanna recreate a situation in which you first create every single html in a web page, and append to some of the html tags events associated to a particular class -example class Person-. for example: Supposed the next code:
var someFunction = function(someText){alert(someText);}
function SomeClassFunction(){
this.aClassFunction = someFunction;
}
var aVariableName = new SomeClassFunction();
and in the HTML code suppose I have the next piece of code.
<html>
<head>
<title></title>
</head>
<body>
<div onclick="aVariableName.aClassFunction('Some text to show in alert');">
</div>
</body>
</html>
Then, as you may notice the onclick event uses the aVariableName I created before, but because I first create the name of the variable and then append the name in the code cause I knew aVariableName was the name of that object. What I wanna do or implement is to create the text above in html without know the variable name of an specific object. I have surfed on the net but, unfortunately I haven't found anything about it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不知道如何从代码本身获取变量的名称,而不需要做大量的解析工作,这会变得混乱,我会为此开枪打死某人。
您知道可以在 javascript
someElement.onclick = someFunction
中设置类似的事件,因此如果您所做的只是设置事件处理程序,则实际上不需要知道变量的名称。i dont know how to get the name of a variable from the code its self without doing a whole load of work parsing stuff, which will get messy, and i'd shoot someone for this.
You know you can set events like this in javascript
someElement.onclick = someFunction
so you dont really need to know the name of the variable if all you're doing is setting an event handler.一般来说,不可以——您也无法获取特定变量的名称或其“id”。
如果你真的愿意,也许可以用异常和堆栈跟踪来做坏事来获取这些信息……但我不知道该怎么做。
编辑:我假设“变量 ID”是指“该变量引用的对象的唯一标识符”,就像 Python 的
id
内置函数一样:In general, no — you can't get the name of a particular variable or its "id" either.
If you really want to, it may be possible to do Bad Things with exceptions and stack traces to get that information… But I don't know off the top of my head how to do that.
Edit: I assume that, by “variable ID”, you mean “a unique identifier for the object referenced by that variable”, like Python's
id
builtin:我不能 100% 确定我正确理解您关于元编程的问题,但通常您会将事件处理程序附加到 DOM 元素的单击事件,然后您可以检查处理程序中元素的属性。
JavaScript 中有一些功能可以促进元编程:eval 可以让您解释任意代码,因此您可以构建一串代码并对其进行评估。安全问题有很多。您还可以通过索引或名称访问对象的属性,例如
与
希望有帮助。
I'm not 100% sure that I understand your question correctly in regards to meta-programming, but typically you would attach an event handler to the click event of the DOM element, then you can examine properties of the element within the handler.
There are a couple things in JavaScript that facilitate meta-programming: eval will let you interpret arbitrary code, so you can build a string of code and eval it. The security concerns are numerous. You can also access properties of an object by index or by name, e.g.
is the same as
Hope that helps.