0

I am writing a program whereby a user can define JS variables, as they are basically writing JS into the program (much like a tutorial) and I would like to keep an array of the variable names. For example, I have an object called Particle, which is defined as the following:

function Particle(x, y, force, angle) {
this.force = new Force(force, angle);
this.pos = new Position(x,y);
}

And if the user creates one with the following syntax:

var bus = new Particle(0,0,4,45)

I would like to be able to append 'bus' to an array, however I do not know how to get the name of the variable from within the variable. There is probably something major I am overlooking, however does anyone know how to retrieve this?

user1150512
  • 259
  • 1
  • 9
  • what do you mean by "from within the variable"? – intuitivepixel Jul 28 '13 at 20:47
  • 1
    I don't think this is possible. Also `new Particle()` can be called without assigning it to a variable(name). And you can even have more than one variable(name) that is assigned that object. `var car; var bus = car = new Particle();`. Which name should you then get from within that object? – nl-x Jul 28 '13 at 20:54
  • What do you need that for? It's impossible and I don't even know why one may be looking for such a feature. You can manage all created objects of your "class", but names of corresponding variables woudn't give you anything usefull because your object can be in local variable or somewhere deep in multidimensional array. – Artem Sobolev Jul 28 '13 at 21:02
  • What is your use case? Why don't you add an attribute name to the Particle function? – Derick Leony Jul 28 '13 at 21:38

0 Answers0