0

我有一个对象:

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

如何设置诸如 location.name = location 之类的内容,以便以后可以访问“位置”字符串?

像这样的东西会起作用吗?

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

提前致谢!

4

2 回答 2

6
location = {
    place1: distance1,
    place2: distance2,
    name : "location"
}
于 2013-06-26T15:24:08.407 回答
0

在 js 中几乎一切皆有可能:)

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

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

另一种方式:

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);
于 2013-06-26T15:33:30.660 回答