gevik
gevik

Reputation: 3427

How do object references work in JavaScript

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.

Upvotes: 2

Views: 138

Answers (1)

Deepak Ingole
Deepak Ingole

Reputation: 15772

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 () {}
            }
        }
    } }

Upvotes: 3

Related Questions