最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript multiple keys to the same value - Do they point to the same place? - Stack Overflow

programmeradmin2浏览0评论

I have the following simplified code:

var obj = {
    key1 : {
            aliases: ["alias1", "alias2"],
            prop1: someVal,
            prop2: someOtherVal
        }
}
var objHashMap = {};
for(var key in obj){
    objHashMap[key] = obj[key];
    objHashMap[obj[key].aliases[0]] = obj[key];
    objHashMap[obj[key].aliases[1]] = obj[key];
}

Now objHashMap has 3 entries, and all entries points at:

{
        aliases: ["alias1", "alias2"],
        prop1: someVal,
        prop2: someOtherVal
}

My question is weather all 3 points to the same object, or points to 3 different copies of the object?

I have the following simplified code:

var obj = {
    key1 : {
            aliases: ["alias1", "alias2"],
            prop1: someVal,
            prop2: someOtherVal
        }
}
var objHashMap = {};
for(var key in obj){
    objHashMap[key] = obj[key];
    objHashMap[obj[key].aliases[0]] = obj[key];
    objHashMap[obj[key].aliases[1]] = obj[key];
}

Now objHashMap has 3 entries, and all entries points at:

{
        aliases: ["alias1", "alias2"],
        prop1: someVal,
        prop2: someOtherVal
}

My question is weather all 3 points to the same object, or points to 3 different copies of the object?

Share Improve this question edited Nov 30, 2015 at 10:04 TBE asked Nov 30, 2015 at 9:42 TBETBE 1,1331 gold badge11 silver badges33 bronze badges 1
  • Since obj[key] is an object, they all will reference to the same object. – Yeldar Kurmangaliyev Commented Nov 30, 2015 at 9:48
Add a ment  | 

2 Answers 2

Reset to default 8

objHashMap.key1 will have a copy of the value in obj.key. That value is a reference to an object. Both copies of that value refer to the same object. The you use the value of obj.key1.aliases[0] and obj.key1.aliases[1] to create two additional properties which also have copies of the reference, referring to the same object.

After this:

var obj = {
    key1 : {
            aliases: ["alias1", "alias2"],
            prop1: someVal,
            prop2: someOtherVal
        }
}

...we have this in memory:

                +----------------+
obj: ref-1654-->|    (object)    |
                +----------------+     +-------------------+
                | key1: ref-8754 |---->|     (object)      |
                +----------------+     +-------------------+     +-------------+
                                       | aliases: ref-6549 |---->|  (array)    |
                                       | prop1: ???        |     +-------------+
                                       | prop2: ???        |     | 0: "alias1" |
                                       +-------------------+     | 1: "alias2" |
                                                                 +-------------+

That is, we have a variable, obj, which contains a value that tells the JavaScript engine where an object is elsewhere in memory; I've shown that as ref-1654 above. Think of an object reference as a number that has meaning only to the JavaScript engine, like an index into a big array of memory. (The actual value of an object reference is something we never see.)

That object has a property, key1, which again has a value pointing to an object elsewhere in memory. That object, in turn, has aliases with another reference to an object (this time an array) in memory. The values of prop1 and prop2 in the object with aliases are unknown (they e from the variables someVal and someOtherVal, which you haven't defined for us).

Then this line adds another variable pointing to another object:

var objHashMap = {};
                       +----------------+
objHashMap: ref-8132-->|    (object)    |
                       +----------------+
                       |                |
                       +----------------+

Your for-in loop only runs once, for the key "key1". After the first line:

objHashMap[key] = obj[key];

we have:

                +----------------+
obj: ref-1654-->|    (object)    |
                +----------------+          
                | key1: ref-8754 |---------+
                +----------------+         |
                                           |
                                           |
                                           |   +-------------------+                 
                                           +-->|     (object)      |                 
                                           |   +-------------------+     +-------------+
                                           |   | aliases: ref-6549 |---->|  (array)    |
                       +----------------+  |   | prop1: ???        |     +-------------+
objHashMap: ref-8132-->|    (object)    |  |   | prop2: ???        |     | 0: "alias1" |
                       +----------------+  |   +-------------------+     | 1: "alias2" |
                       | key1: ref-8754 |--+                             +-------------+
                       +----------------+

Note how the key1 property in the new object contains the same value as the key1 property in the original object. They point to the same object.

Then you do:

objHashMap[obj[key].aliases[0]] = obj[key];

...which is to say

objHashMap[obj.key1.aliases[0]] = obj[key];

...since key contains "key1", which is to say

objHashMap["alias1"] = obj[key];

...because obj.key1.aliases[0] is "alias1". That gives us:

                +----------------+
obj: ref-1654-->|    (object)    |
                +----------------+          
                | key1: ref-8754 |-----------+
                +----------------+           |
                                             |
                                             |
                                             |    +-------------------+                 
                                             ++-->|     (object)      |                 
                                             ||   +-------------------+     +-------------+
                                             ||   | aliases: ref-6549 |---->|  (array)    |
                       +------------------+  ||   | prop1: ???        |     +-------------+
objHashMap: ref-8132-->|     (object)     |  ||   | prop2: ???        |     | 0: "alias1" |
                       +------------------+  ||   +-------------------+     | 1: "alias2" |
                       | key1:   ref-8754 |--+|                             +-------------+
                       | alias1: ref-8754 |---+ 
                       +------------------+

Then again for this line:

objHashMap[obj[key].aliases[1]] = obj[key];

...which is:

objHashMap["alias2"] = obj[key];

...because obj.key1.aliases[1] is "alias2". So we end up with:

                +----------------+
obj: ref-1654-->|    (object)    |
                +----------------+          
                | key1: ref-8754 |-----------+
                +----------------+           |
                                             |
                                             |
                                             |    +-------------------+                 
                                             +++->|     (object)      |                 
                                               +-------------------+     +-------------+
                                               | aliases: ref-6549 |---->|  (array)    |
                       +------------------+    | prop1: ???        |     +-------------+
objHashMap: ref-8132-->|     (object)     |    | prop2: ???        |     | 0: "alias1" |
                       +------------------+    +-------------------+     | 1: "alias2" |
                       | key1:   ref-8754 |--+||                            +-------------+
                       | alias1: ref-8754 |---+|
                       | alias2: ref-8754 |----+ 
                       +------------------+

Yes, these point to the same object. Let me take an example to demonstrate the same:

  var obj = {
      key1 : {
              aliases: [1,2],
              prop1: 3,
              prop2: 4
          }
  }
  //initialize another object which will contain the same values.
  var objHashMap = {};
  for(var key in obj){
      objHashMap[key] = obj[key];
      objHashMap[obj[key].aliases[0]] = obj[key];
      objHashMap[obj[key].aliases[1]] = obj[key];
  }
  //change the value of a key in objHashMap.
  objHashMap["key1"].prop1 = 600
  //now observe the value changed in other keys as well.
  console.log(objHashMap[1].prop1);
  console.log(objHashMap[2].prop1);
  console.log(obj);
发布评论

评论列表(0)

  1. 暂无评论