简体   繁体   中英

Creating and returning object from function: can this avoid garbage collection?

If I have a function that creates an object, do some stuff and than returns it. If it's true that objects are passed by reference, does this mean that the function that creates the object (or the function's scope chain) will not be available for garbage collection?

Example code:

function convertArrayToObj(array){
   var newObj = {};
   array.forEach(function(item, index){
      newObj[index] = item;
   });
   return newObj;
}

I hope I made my doubt clear

It (the execution context) will be eligible for garbage collection.

(The function object itself will be kept alive of whoever called it to begin with, until that object dies - but it will not be kept alive by the returned 'newObj'.)

Just because a reference to the object is stored in a local variable does not mean that the object has got a reference back to its creator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM