1

I have the following code:

function createPersone(gender){
 let localPerson = new Object();
 localPerson.gender = gender;
 return localPerson;
}

let globalPerson = createPerson("female");

In the function is created a reference to the object, localPerson contains the address where the created Object is in the memory. When localPerson is returned this address is copied into globalPerson. However localPerson has scope function thus it should be destroyed but its content is an address, is the one to whom the address points (namely the object) also deleted? Deletion of object should be gouverned from garbage collection. In this case can I assume like safe the instruction let globalPerson = createPerson("female"); and why? What happen behind the hood?

1
  • Yes localPerson is destroyed but the object remains, since it's still referenced by globalPerson. If that didn't work, then probably half (if we are being very modest) of all JS code ever would just fail, since a function execution would finish and any objects it had will be cleared out even if they shouldn't be. Commented Jan 23, 2020 at 18:46

1 Answer 1

3

As long as you have a way to access some value, that value will not be garbage collected. That is, for any correctly-implemented garbage collector for JavaScript, you will never find a value destroyed by the garbage collector as long as you could access it.

Since no functions are created and returned from createPerson, it is true that the closure created by calling createPerson will be garbage collected, so the closure-local variable localPerson will be destroyed. However, the value that that variable referred to will not be destroyed, because it is still accessible via the globalPerson variable.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok I understand and I supposed this behavior. However in the unfortunate but not impossible case that the garbage collect goes into action when the function has already executed but before the assignement to the reference globalPerson (I think this is possible or am I wrong?) the object is unreferenced and in this remote but not impossible case could be erased or am I wrong (for instance bacause assignement is an atomic operation)?
@Umbert Any JavaScript garbage collector that causes observably different behavior (i.e., there is ever any observable difference in program behavior when using vs not using that GC) is not a correctly-implemented JavaScript GC. JavaScript garbage collectors may not ever alter program execution; they may only destroy objects and constructs that cannot possibly ever be used again. Since the object can be used again (and indeed is about to be used for the value of globalPerson) it cannot legally be destroyed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.