2

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.

Anurag
  • 140,337
  • 36
  • 221
  • 257
crsuarezf
  • 1,201
  • 3
  • 18
  • 33

3 Answers3

0

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)
David Wolever
  • 148,955
  • 89
  • 346
  • 502
0

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.

flatline
  • 42,083
  • 4
  • 31
  • 38
  • Yep, as you and jasper said, We all agree the possibility of attach event handlers to vars, and this is typically the first thing we would use in pro of our code, but this also affect the point of what I want to do. Using a lot of meta-classes, or singleton classes as -ruby concept- is for what I want to do, the wrong an impossible way of doing that. Imagine for a moment that javascript hasn't built in anonymous class -singleton, eigen, meta-class- how could you do it that. The only thing squeeze my mind is download webkit and try to implement such thing in the javascript interpreter. – crsuarezf Nov 05 '10 at 19:58
0

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.

jasper
  • 3,424
  • 1
  • 25
  • 46
  • What you said is perfectly right, however, I'm looking for the option I mentioned in my question. I wanna do some automated code, perhaps what you said works and works well, but, think in this point: I don't wanna do deeply anonymous functions. I wanna try other way. But what you said really works, and works nice. If I don't find anything else, I think I'm gonna have to use what you are pointing. – crsuarezf Nov 05 '10 at 19:32