javascript - Referencing global variables in local scopes -


I have to know the memory leak in the code mentioned below. Does Javascript store automatic garbage?

var aGlobalObject = SomeGlobalObject; Function myFunction () {var localVar = aGlobalObject; }

Do I have to clear the memory given below.

  var aGlobalObject = SomeGlobalObject; Function myFunction () {var localVar = aGlobalObject; LocalVar = null; // local version delete  

thanks

You have that code There is no memory leak in the local variable references the same object as the global variable. When the function returns, the local variable is deleted because nothing is referenced for it. The object remains, as it is still referred to by the global variable. ( When I say "local variables have been removed": Technically, the [underlying] container is no longer the reference of the variable and the garbage is available for storage; the actual collection can be later. )

Javascript is actually a garbage collection Delete means something different than completely javascript, say, C ++ Javascript objects have properties in you by using delete Remove a property completely Isi object, eg .:

  var obj = {}; // empty object obj.foo = 5; // `obj` now has a property which is called 'foo' oz Fu = null; `` Obj` is still an asset called 'Fu'; Delete obj.foo; its value is still zero; // `oboje` no longer has any property that` foo ' 

Comments