Home > Enterprise >  If I return an object is it a reference or not?
If I return an object is it a reference or not?

Time:10-20

I'm pretty new to the world of software but have read and experience that when you have an object like:

const obj1 = {key1: "val1", key2: "val2"};

// and then do this =>
const obj2 = obj1;
// obj2 isn't a new object but a reference to object 1

// which you can demonstrate with 
console.log(obj1);
obj2.key1 = "value3";
console.log(obj1);
// now key1 of object 1 has changed

But I'm kind of wondering what happens in the following situation.

const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);

function getUrlParams(_urlParams) {
    let retObj = new Object();

    _urlParams.forEach(function(value, key) {
        retObj[key] = value;
    });

    return retObj;
}

const urlArgs = getUrlParams(urlParams);

I couldn't really figure out a way to test if urlArgs is now a reference to retObj or not. I doubt it is since retObj was defined inside of the function and should no longer exist when the function is done. But with what I know of javascript I'm not really sure.

After reading some comments is there a way to test if

urlArgs === retObj;
// If urlArgs and retObj are both referencing the same object?

CodePudding user response:

There's some basic knowledge that you are missing, how references work.

Two objects

For example: console.log({ age: 42 } === { age: 42}); => return false.

Note that in order to compare references of objects you should use ===.

The two object have the same values but they are not the same object in memory. In your example, you are coping all the properties of urlParams, both object will "look" the same, but will not be the exact same location in memory.

Same object example:

const person1 = { age: 42 }; // Creating a new object in memory, and pointing "person1" to it
const person2 = person1; // creating another point to the same object.
console.log(person1 === person3); // ==> returns true

Are you can see, when creating an object like this: const person1 = { age: 42}; does 2 things, creating an object and a reference to it. on the next line const person2 = person1; only a new reference to the same memory location is created.

That's why, when changing the age of one of the references will be reflected in the other reference (same memory)

primitives

Another important thing to understand is that primitives (numbers, booleans, strings, symbol, and bigint) are not references.

const a = 42;
const b = a; // this is a COPY, not a reference

Releasing the memory

In Javascript all your variable are "alive" (In memory) as long as there's a reference to them. when you define a variable in a function, this variable is "alive" in the scope of the function. after this function execution is done, the variable is released.

In short, when all references are gone, the memory is released automatically. (this called garbage collection).

CodePudding user response:

In this scenario retObj is more class like in that it is an Object making factory. Each time you pass in parameters you are building a completely different object with you retObj factory and after the function is done running retObj is removed from memory.

If you moved retObj outside of the getUrlParams function like so

const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);

let retObj = new Object();

function getUrlParams(_urlParams) {

    _urlParams.forEach(function(value, key) {
        retObj[key] = value;
    });

    return retObj;
}

const urlArgs = getUrlParams(urlParams);

you could then play around with it but at this point it would be on the global level and any changes to the data would propagate up/down since retObj would have a dedicated space in memory making urlArg a reference to retObj.

  • Related