0

Possible Duplicate:
Determine original name of variable after its passed to a function
Get the reference name of an object?

Not exactly sure how to explain this. I'm looking to get a string value of the variable name, in an object itself.

For instance:

var MyObject = function(){
  var that = this;

  that.alertMyName = function(){
    var name_of_instance = that.__name__;
    alert(name_of_instance); //hoping to retreive a string containing 'var1'?
  }

};

var var1 = new MyObject();
var1.alertMyName();
Community
  • 1
  • 1
ndmweb
  • 3,370
  • 6
  • 33
  • 37
  • 2
    Food for thought: Given this code, what is the "name" of the instance? `var var1 = new MyObject(); var var2 = var1; var1.alertMyName(); var2.alertMyName();` `var1` and `var2` both point to the *same object*. – cdhowie Jun 13 '12 at 23:13
  • Note that tying logic to variable names is almost always (I say *almost*, but I have yet to see a valid use case) a terrible idea. – Ed S. Jun 13 '12 at 23:13
  • There is a good discussion in this question: http://stackoverflow.com/questions/789675/how-to-get-class-objects-name-as-a-string-in-javascript – sachleen Jun 13 '12 at 23:14
  • https://developer.mozilla.org/en/JavaScript/Reference/Functions_and_function_scope/arguments/callee – powtac Jun 13 '12 at 23:15
  • http://eriwen.com/javascript/js-stack-trace/ – powtac Jun 13 '12 at 23:18

3 Answers3

3

It is not possible to determine the name(s) of any variable(s) an object is referenced from, not in this particular example anyway. Think of the case where you have many variables referencing the same object -- what would you expect to be returned in that case? What if no variable references the object at all? (Consider the expression new MyObject().alertMyName() -- what should that object's name be?)

If you want to give an object a name, make it explicit:

var MyObject = function(name){
  var that = this;

  that.alertMyName = function(){
    alert(name);
  }

};

var var1 = new MyObject("var1");
var1.alertMyName();
cdhowie
  • 158,093
  • 24
  • 286
  • 300
0

if you do a

for (x in obj){}

on an object in javascript, x holds the name of the object attribute/variable name, i believe you can get what you want that way :)

Ryan
  • 2,755
  • 16
  • 30
  • No, you can't. This will enumerate the properties of the object, and will not contain the name of any variable(s) the object is referenced from. – cdhowie Jun 13 '12 at 23:14
  • I see, your point, your point above is different that the one i was making. You could create a name property on each instance though that would solve their problem, as in new MyObject("name"). – Ryan Jun 13 '12 at 23:18
  • 1
    @Ryan It would *redefine* their problem, but not solve the one stated :) – Dave Newton Jun 13 '12 at 23:18
  • fair enough... just tryin to help out :) – Ryan Jun 13 '12 at 23:20
0

Oh sure, this is easy (as in practically impossible). Just print out the code, and then read down to the function call, and then write down the variable name.

In code that becomes a little more complex, however still doable in an edge case. You would have to parse through the document which calls the function which would require explicit knowledge of where that document resides. Using an $.ajax call you could load the entire calling script or page into a variable and then parse with a sliding window until you found the function call with the variable name used. However, you would then have to ensure that the function call found was the one which correlated to the current instance.

Travis J
  • 81,153
  • 41
  • 202
  • 273