JavaScript 获取变量名

发布于 2024-09-30 20:43:33 字数 843 浏览 3 评论 0原文

这是我的第一篇文章。

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

染年凉城似染瑾 2024-10-07 20:43:33

我不知道如何从代码本身获取变量的名称,而不需要做大量的解析工作,这会变得混乱,我会为此开枪打死某人。

var someValue;
var foo = function() { someValue; }
alert((foo + '')); // this is now a string, use substr to extract variable name

您知道可以在 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.

var someValue;
var foo = function() { someValue; }
alert((foo + '')); // this is now a string, use substr to extract variable name

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.

电影里的梦 2024-10-07 20:43:33

一般来说,不可以——您也无法获取特定变量的名称或其“id”。

如果你真的愿意,也许可以用异常和堆栈跟踪来做坏事来获取这些信息……但我不知道该怎么做。

编辑:我假设“变量 ID”是指“该变量引用的对象的唯一标识符”,就像 Python 的 id 内置函数一样:

>>> x = {}
>>> y = z = {}
>>> (id(x), id(y), id(z))
(123, 567, 567)

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:

>>> x = {}
>>> y = z = {}
>>> (id(x), id(y), id(z))
(123, 567, 567)
习惯成性 2024-10-07 20:43:33

我不能 100% 确定我正确理解您关于元编程的问题,但通常您会将事件处理程序附加到 DOM 元素的单击事件,然后您可以检查处理程序中元素的属性。

JavaScript 中有一些功能可以促进元编程:eval 可以让您解释任意代码,因此您可以构建一串代码并对其进行评估。安全问题有很多。您还可以通过索引或名称访问对象的属性,例如

aVariableName.aClassFunction

aVariableName["aClassFunction"]

希望有帮助。

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.

aVariableName.aClassFunction

is the same as

aVariableName["aClassFunction"]

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文