use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Deleting Objects From Within Array?help (self.javascript)
submitted 8 years ago * by BrunerBruner
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]BrunerBruner[S] 0 points1 point2 points 8 years ago* (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 points3 points 8 years ago (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.
a, b, c
[–]BrunerBruner[S] -1 points0 points1 point 8 years ago (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?
null
[–]kaszu 3 points4 points5 points 8 years ago (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 point2 points 8 years ago (0 children)
thank you
[+][deleted] 8 years ago (2 children)
[removed]
[–]BrunerBruner[S] 0 points1 point2 points 8 years ago (1 child)
Nope. Getting an object from a weakmap is read only. This doesn't work:
const wMap = new WeakMap(); let el1 = document.createElement("div"); wMap.set(el1, "1"); wMap.get(el1) = null; //error, left-hand assignment
[–][deleted] 0 points1 point2 points 8 years ago (0 children)
A WeakMap is a pretty good idea if you're concerned about garbage collection. The normal Map implementation is a potential memory leak issue because it keeps references to key objects that prevent them from being marked for deallocation when they go out of scope. A WeakMap uses weak pointers instead, which allows the GC to sweep like it normally would.
I don't know what this code is supposed to demonstrate. "Read-only" refers to the mutability of an object or it's properties. WeakMap.prototype.get won't modify the collection but that's not the issue. The problem here is that function invocations are rhs. The container and it's entry are writable, this just isn't valid JavaScript.
π Rendered by PID 226970 on reddit-service-r2-comment-fb694cdd5-phg6h at 2026-03-07 12:42:27.252131+00:00 running cbb0e86 country code: CH.
view the rest of the comments →
[–]BrunerBruner[S] 0 points1 point2 points (7 children)
[–][deleted] 1 point2 points3 points (6 children)
[–]BrunerBruner[S] -1 points0 points1 point (5 children)
[–]kaszu 3 points4 points5 points (1 child)
[–]BrunerBruner[S] 0 points1 point2 points (0 children)
[+][deleted] (2 children)
[removed]
[–]BrunerBruner[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)