0

I have an object:

location = {place1: distance1, place2:distance2};

How do I set something like location.name = location, so that I can access the "location" string later on?

Would something like this work?

location = {
    place1: distance1,
    place2: distance2
    location.name = "location";
}

Thanks in advance!

4
  • 1
    i dont get what youre trying to do. just set the name? Commented Jun 26, 2013 at 15:24
  • Do you want to dynamically set the property name? Commented Jun 26, 2013 at 15:25
  • 3
    window.location has meaning! You need to pick a different variable name if it is a global variable Commented Jun 26, 2013 at 15:26
  • Are you trying to store a reference to the variable location inside the object? Commented Jun 26, 2013 at 15:27

2 Answers 2

6
location = {
    place1: distance1,
    place2: distance2,
    name : "location"
}
Sign up to request clarification or add additional context in comments.

1 Comment

I suspect there's more to the question than this, but enjoy the easy rep in the meantime! :)
0

Almost everything is possible in js :)

my_location = {
    place1: distance1,
    place2: distance2
};
my_location.name = "location";

alert(my_location.place1 + ' ' + my_location.place2 + ' ' + my_location.name);

Another way:

function MyLocation(distance1, distance2){
    this.place1 = distance1;
    this.place2 = distance2;
}
my_location = new MyLocation(dist1, dist2);
my_location.name = "location";
alert(my_location.place1 + ' ' + my_location.place2 + ' ' + my_location.name);

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.