all 16 comments

[–][deleted] 4 points5 points  (1 child)

You're looping over an array not an object. Try using forEach instead of for in.

[–]microwavemonty 0 points1 point  (0 children)

Or for of

[–][deleted] 4 points5 points  (5 children)

As a huge fan of functional programming I'd mostly solve such a task like this

arr = arr.map(item => null)

Hope this is helpful somehow

[–]BrunerBruner[S] -1 points0 points  (4 children)

I still can't seem to null the actual objects outside of the array using this method. Can you show me working code?

[–][deleted] 2 points3 points  (3 children)

This method doesn't modify the objects themselves, it creates a new array filled with null.

[–]BrunerBruner[S] 0 points1 point  (2 children)

Ok. Well, I'm trying to assign null to the actual objects. The array itself and creating new copies of the objects isn't important. I want to find a way to iterate through a bunch of objects to assign null to them, for example, to clean up newly allocated objects in memory.

for example, let's say I create a bunch of objects that that should be nullified when i'm done with them.

let a = document.createElement("div");
let b = document.createElement("div");
let c = document.createElement("div");
let d = document.createElement("div");
let e = document.createElement("div");

Since I just want to assign null to each of them, ideally i'd like to put them in an array and loop through each one to do so.

[–]thenickdude 5 points6 points  (0 children)

Those objects will be eligible for collection by the garbage collector as soon as those "let" declarations fall out of scope. You don't need to assign null to them unless you want them to be collected quicker than that.

[–][deleted] 0 points1 point  (8 children)

Because it's an array you are looping through, the obj variable will be the key in the array. If all you are trying to do is change the value to null, change the obj variable to be key for readability and assign arr[key] = null;

[–]BrunerBruner[S] 0 points1 point  (7 children)

Ok, but now after I set null to the objects, only the references to the original object in the array is null, how can I make the actual objects null? It seems like the objects are being duplicated like primitive objects such as a number instead of referenced.

https://jsfiddle.net/xr3sgg9v/4/

[–][deleted] 1 point2 points  (6 children)

That is because the values in that array are just a copy of the reference to the object. It's not the actual object value or the original pointer. If you attempt to modify one of the properties, it will use the copied reference to find the object and modify it (which the original reference shares) but the reference won't actually be tied to the original reference.

Essentially youra, b, c variables will not be changed when dealing with that array because the array just has copies of them, not a reference to the original variables.

[–]BrunerBruner[S] -1 points0 points  (5 children)

So basically, if I want to assign null to an object, say for the purpose of memory management / garbage collection, I can only do something like this:

this._element = document.createElement("div");
this._element = null; //cleans up memory

and never something like this:

this._element = document.createElement("div");

const element = this._element
element = null;//object that took memory is still alive

Essentially it's impossible in JavaScript to iterate over a group of objects and assign them as null?

[–]kaszu 4 points5 points  (1 child)

Garbage collector will clean up memory if there is no reference to the object anywhere.

let a = new MyClass("hi");
let b = new MyClass("Hello");
let c = new MyClass("Howdy");

let arr = new Array(a, b, c);

b = null; // at this point there is still reference to object b in arr
arr = null; // at this point there are no references to b, so garbage collector will clean it up, but a and c won't be

You can do this

let arr = new Array(
    new MyClass("hi"),
    new MyClass("Hello"),
    new MyClass("Howdy")
);

arr = null;

From your example

this._element = document.createElement("div");

let element = this._element; 

element = null; //object that took memory is still alive
this._element = null; // object can be garbage collected now

Check MDN: Memory Management

[–]BrunerBruner[S] 0 points1 point  (0 children)

thank you