2

I created an object called Location:

function Location(street,city,postal,phone,fax, lat, lon, distance){
    this.street = street;
    this.city = city;
    this.postal = postal;
    this.phone = phone;
    this.fax = fax;
    this.lat = lat;
    this.lon = lon;
    this.distance = distance;
}

Then constructed each of the objects:

example1 = new Locaiton('value', 'value', etc)
example2 etc etc

Then stored the objects in an array

var cities = [example1, example2]

I tried using cities[0].constructor.name, but this just prints out Location.

How do I print out "example1" so that I can compare it with a matching element id in an if statement?

7
  • 1
    Why do you need to do this? Commented Jan 31, 2014 at 22:01
  • 4
    You can't do this. Unless you save the variable's name somewhere, you can't get it. Commented Jan 31, 2014 at 22:02
  • @p.s.w.g just edited to explain Commented Jan 31, 2014 at 22:03
  • So, you aren't trying to print the object name, as the title implies, but actually the variable name. Commented Jan 31, 2014 at 22:03
  • If your naming follows that convention you can use the index in the array... "example"+(i+1) Commented Jan 31, 2014 at 22:04

6 Answers 6

3

example1 is a reference to your Location object, and JavaScript has no concept of doing what you want to do.

If you really need the name, I would store it on your object.

Sign up to request clarification or add additional context in comments.

Comments

3

Objects don't have any back-reference to the variables that refer to them. Consider this:

var example1 = new Location(...);
var example2 = example1;

Now there's one location named by two variables, which would you expect the "name" to be?

Comments

0

May I suggest that rather storing the set of locations as an array, you store it as an object. You appear to be trying to store the name of a city in combination with the location data.

You could use a data structure like this for cities:

var cities = {
    "london": example1,
    "lisbon": example2
}

You can then do:

cities.london.name

You have to add return this to the end of your Location function definition.

Comments

0

You can't do what you want. Objects in JavaScript are generally used by reference rather than by value. So for, example, if you used:

var ex1 = new Location();
var ex2 = ex1;
var ex3 = ex1;

Then all three of those variables (ex1, ex2, and ex3) are referring to the exact same object. There's no way that the object could pick one even if the language tried to permit it.

Comments

0

You could store the objects in another dynamic object, and then use the "variable name" as the key, but I'm not 100% sure that that buys you much. The approach would be:

var examples = {};
examples["example1"] = new Location('value', 'value', etc);
examples["example2"] = etc etc

At that point, you would be able to access the key associated with the current object. The thing is, you would have to have access to the key in order to get to the object, so, like I said, I'm not sure how much it buys you . . .

Note: Chris Alexander and Jay are suggesting similar approaches . . .

1 Comment

That's why I mentioned you (and Chris) at the bottom . . . apparently we were all typing at the same time. :)
0

You're wanting it to print out the name of the variable it would appear. This is actually not possible.

If you need to do this, you have a couple of options. The two best options are probably:

1) Add some kind of name function to the Location. Then add a "toString()" function which returns the string. Then, if you use it in places where it needs to convert it to a string, it'll use whatever the output of toString is.

2) Instead of storing them in an array, store them in an object where the key is the name you want. Then you can use the key instead.

var locations = {
    example1: new Location(),
    example2: new Location()
}

for (var name in locations) { console.log(name); } // logs "example1" and "example2"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.