2

I was wondering if someone could shine a little light on the following subject.

I have "heavy" object with lots of functions and properties:

var HeavyObject = {
   property1:{}
   property2:1,
   propertyN:false,
   func1:function(){}
   func2:function(){}
   ....
   funcN:function(){}
}

Next I have a "lightweight" object:

var LightWheight =  {
   property1: {
          sub_property:HeavyObject
   },
   property2: {
          sub: {
               sub:{
                   flag:true,
                   heavy:HeavyObject
               }
          }
   }
}

My question is how heavy my "LightWeight" object actually is since it has two references to the "HeavyObject"? Does it only keep two pointers to the "HeavyObject" or is the reference done differently?

I understand that this might vary based on the JavaScript engine, but I would like to understand the general idea.

Any clarification is greatly appreciated.

gevik
  • 3,177
  • 4
  • 25
  • 28
  • 5
    They're references, i.e. it doesn't store the whole "heavy object" inside. – Ja͢ck Feb 14 '14 at 07:30
  • 1
    [look here][1] - it is quite will described. [1]: http://stackoverflow.com/questions/6605640/javascript-by-reference-vs-by-value – i100 Feb 14 '14 at 07:33

1 Answers1

3

Does it only keep two pointers to the "HeavyObject" or is the reference done differently?

It keeps two pointers to HeavyObject.

If you want to really test it change any of the two pointers and check values of both pointer they pointing to .

Your LightWheight will become heavy if you do,

var LightWheight = {
    property1: {
        sub_property: {
            property1: {}
            property2: 1,
            propertyN: false,
            func1: function () {}
            func2: function () {}....
            funcN: function () {}
        }
    },
    property2: {
        sub: {
            sub: {
                flag: true,
                heavy: property1: {}
                property2: 1,
                propertyN: false,
                func1: function () {}
                func2: function () {}....
                funcN: function () {}
            }
        }
    } }
Deepak Ingole
  • 14,912
  • 10
  • 47
  • 79