+ 15
Javascript Object deletion
I made a small example code and was wondering if the object I'm creating in foo is deleted in baz or does it still exist because it was referenced in foo and bar? If if still exists, how can I delete it? https://code.sololearn.com/WNfwBORaHIsm/?ref=app Also another question in the code here: https://code.sololearn.com/Wmaj88lzVwCC/?ref=app
15 Respostas
+ 5
[Part 1 of 2]
Gordon: Thanks for the ping. I'll try to explain this the way my mind parses code like this.
Martin: I'll start by responding to your 2nd code:
https://code.sololearn.com/Wmaj88lzVwCC/?ref=app#js
Let's breakdown what is essentially occurring with specific lines.
-----
Line 5: var a = new A();
-----
1. Memory is allocated for a new {instance of A}:
> [0x001] | {instance of A}
2. Memory is allocated for string variable {A.a} and assigned value "a":
> [0x002] | {A.a} -> "a"
3. Memory is allocated for reference variable {a}:
> [0x010] | {a}
4. The memory address of {instance of A} is stored as the value for {a}.
> [0x010] | {a} -> [0x001] | {instance of A}
NOTE: {a} is a pointer reference to the memory address for the {instance of A}.
+ 9
I don't know much but in JavaScript objects are perseved till you have reference to it if there is no reference to any object then the object is deleted automatically and I guess there is no way to manually delete objects in js
You can simply do this
a = null ;
// to remove the reference
Object A() will be deleted if there was no other reference to it
I hope it helps
+ 9
For the second one I guess you want to deep copy objects in that case new objects are separate from each other (and the other one is swallow copy in which they point to same object)
I found a nice artical about
https://medium.freecodecamp.org/copying-stuff-in-javascript-how-to-differentiate-between-deep-and-shallow-copies-b6d8c1ef09cd
+ 9
Martin
Yes you are right
But copy of objects is also swallow
In memory there is only one object and we have two reference for it "a" and "b"
By deleting b we are only deleting the memory address of the real object stored in b not the actual object thats why deleting b do not effect a
+ 8
Check this
I made it from that article
https://code.sololearn.com/WiCr6lVLEyqV/?ref=app
+ 7
Bad_Bit Sorry, my fault ^^ In line 20 I meant a instead of b. But thanks for the answer đ
+ 7
oh sorry
that was the answer for your question inside the code...
yes if you delete the reference object will be deleted
+ 6
Bad_Bit
Yeah I think objects in JS are generally shallow copies if I do like var b = a. Because b.a is the same as a.a. So the properties are shallow copies. But then the object itself doesn't seem to be a shallow copy as deleting/changing b doesn't delete the object đ€
+ 6
[Part 2 of 2]
-----
Line 13: a = undefined;
-----
1. {a} is assigned a reference to memory address of variable {undefined}:
> [0x010] | {a} -> [0x800]|{undefined}
2. New values for memory addresses of {a} and {b}:
> [0x010] | {a} -> [0x800] | {undefined}
> [0x020] | {b} -> [0x001] | {instance of A}
NOTE: Although {a} is now {undefined}, {b} still points to {instance of A}.
-----
Summary of Facts:
-----
1. {a} and {b} point to {instance of A}.
2. {A.a} is shared by both {a} and {b}.
3. Setting {a} with new reference to {undefined} will only change the memory address of {a}. However, the memory address of {b} is still pointing to the memory address of {instance of A}.
4. Garbage Collector (GC) will release memory when no other variables are referencing the memory address for {instance of A}.
Therefore, the following lines will technically make {instance of A} to be eligible to be released.
> a = "a";
> b = "b";
At this point, neither {a} nor {b} have a re
+ 6
Sorry for the very long explanation. It played out really short on my head. đ
However, this can be tricky to understand and I picked up on all the misunderstandings in the comments of Martin's code.
I hope this makes sense and helps.
+ 5
Bad_Bit
Yeah I know about deep and shallow copy đ But how can I actually delete the object that is referenced by a and b, which is my original question đ
đ€ JS deletes objects by garbage collection so it will be deleted when it isn't referenced anymore.. So I would need to do a = null and b = null đ€
+ 5
Interesting. Let me switch on my computer and use some development tool for this.
+ 5
Why sorry for a long explanation? đ€
I'm glad you took the time to answer my question very detailed so that I could understand the JS memory better. And I think I now understand what's happening. Thanks a lot David!
Also thanks for hosting the AMA, it is a pleasure to read through all your helpful answers and see how much time you took to help everyone in detail đđ
+ 2
David Carroll hello David, can you help on this question from Martin?