What happens memory wise when copying member function and variables from one object to another

Xandor

Lets say we have two objects and a using function like this:

function using(namespace, scope){
    for(x in namespace){
        scope[x] = namespace[x];
    }
}

function fooClass(){
    this.foo = function(){
        console.log("foo");
    }

    this.bar = function(){
        console.log("bar");
    }
}

var myFoo = new fooClass();

function barClass(){
    using(myFoo, this);
}

var myBar = new barClass();
myBar.foo();

In this example, it carries over everything from fooClass in to barClass. This is a small example of a vary large scale situation I am looking into doing this. The namespace that I am working with in my actual use case is rather large. I know javscript passes by reference for all objects. Some items from the namespace are arrays of objects or individual objects that contain arrays of objects or simply just functions. Most primitive variables are private so they should not carry over.

I am curious how much data I am duplicating and how much data I am referencing? Objects stored in the namespace should be referenced, but what about functions? Are they copied like a primitive variable or referenced? More so, should I be concerned about memory consumption with this using() function?

The main reason I wrote it was because sometimes I'm having to make a call to a deeply nested object and this makes the code more readable, but I do not want to do this if I am sacrificing memory.

traktor53

When copying namespace properties to space using for ... in, all primitive and object reference values are duplicated so they take up twice as much memory.

JavaScript object values (which include function objects) are held in variables as references to "somewhere" in memory that holds object properties. The copy operation only duplicates the reference, not underlying property names and values or function code, so additional memory consumed by creating a copy is similar to copying a primitive value.

For the use case presented however, you may wish to extend the base myFoo object so it is inherited by barClass instances. This does mean, of course, that no snapshot of mFoo is taken, and myBar instances would inherit any future changes to myFoo.

ES5 example:

let myFoo = {foo: "foo"};

function myBar() {
    this.bar = "bar";
}
myBar.prototype = Object.create( myFoo);
myBar.constructor = myBar;

let bar = new myBar();
console.log( bar.foo);

ES2015 requires a little more work to create an intermediate function to extend an object which is not a function:

let myFoo = {foo: "foo"};
function interFoo() {
};
interFoo.prototype = myFoo;
   
class myBar extends interFoo {
   constructor() {
      super();
      this.bar = "bar";
   }
}

let bar = new myBar();
console.log( bar.foo);

Note that if you extend fooClass directly (the constructor of myFoo), creating instances of barClass would recreate all properties locally by calling forClass as the super class constructor - which is probably not what you want.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Understanding what happens with variables when I call one batch script from another (largely terminology/semantics specific)?

What happens when you "alias" member variables with member function inputs/outputs?

What happens to an activity when another one start?

Copying properties from one object to another with a condition

What happens when Classes that are friends have same name member variables

Undefined Reference when Invoking Function of One Object from Another Object

Expecting member declaration (this happens when using a variable from another class)

What happens to the CPU pipeline when the memory with the instructions is changed by another core?

Calling member function of one class from another

Javascript Object.create: what happens to memory when data is set?

Copying lines from one file to another using sed and shell variables

What happens when you use another function as the argument for the print() function?

What happens on the stack when one value shadows another in Rust?

What happens to a pointer to another pointer when the first one is freed?

Copying object from one Realm to another fails on primary key conflict

Change values when copying records from one table to another

File not found error when copying images from one folder to another

Copying row from one table to another when trigger is called

Error when copying from sheet of one workbook to another

When copying from one workbook to another, an "unwanted" workbook is created

Copying cell values from one sheet onto another when logging on

jQuery.when: What happens if one function fails?

What exactly happens when one calls a 'spawn' function?

what happens if you import a decorated python function from another script?

Retrieve values from one observable when an event happens in another observable

What happens when a function that returns an object ends without a return statement

Accessing variables from one function to another in JavaScript

What happens to the old binary when a new one compiled from source?

What happens exactly when casting from a base class to derived one?

TOP Ranking

HotTag

Archive