Yes, this second snippet will cause "s" to be bound to the local context of the outer function (let's call it "a"). This context will in turn be kept alive by all nested functions, like the one that contains the console log instruction (let's call it "b"). So this means that from within the console log instruction the reachability chain keeping the "hello" string alive will look as follows: "b -> context_a -> s".
In this example the string becomes unreachable, after execution has left the outer function and hence the function itself becomes unreachable. Both are then eligible for collection. Best regards, Michael On Wed, Jun 11, 2014 at 10:57 AM, Mihail Slavchev <[email protected] > wrote: > Hi Michael, > > Thanks for the quick reply. About the scope of "s" - I didn't realized it > will be bound to the global object. Does the following script make a sense? > > (function() { > var s = "hello " > > (function(name) { > console.log(s + name) > })("John") > })() > > My main point is to understand how does V8 bind "s" to the scope of the > inner function? As I said earlier, I guess V8 will introduce some new > property that will hold a reference to the "hello " string. Is that correct? > > Regards, > Mihail > > > On Wednesday, June 11, 2014 11:37:38 AM UTC+3, Michael Starzinger wrote: > >> Using the code snippet verbatim on the top-level (i.e. not nested within >> a closure) will make "s" be bound to a property on the global object. Since >> the global object is shared between all scripts running in the same >> context, there is no way for V8 to determine a limited lifetime for this >> variable and the string will be kept alive forever. >> >> Best regards, >> Michael >> >> >> On Wed, Jun 11, 2014 at 10:24 AM, Mihail Slavchev <[email protected]> >> wrote: >> >>> Hi all, >>> >>> I am reading V8 source code in order to get better understanding how V8 >>> maintains object lifetime during GC. Here is a short example that I am >>> trying to understand. >>> >>> var s = "hello " >>> >>> (function(name) { >>> console.log(s + name) >>> })("John") >>> >>> Obviously, the GC won't collect the string "hello " before executing the >>> function because it is referred from within the function via the "s" >>> variable. I guess V8 creates a temporary object on the fly that actually >>> has a property, say "__s", that holds a reference to the "hello " string. >>> >>> Is that right? I will appreciate if someone points me to the right place >>> in V8 source code where I can learn the details. >>> >>> Thanks in advance, >>> Mihail >>> >>> -- >>> -- >>> v8-users mailing list >>> [email protected] >>> >>> http://groups.google.com/group/v8-users >>> --- >>> You received this message because you are subscribed to the Google >>> Groups "v8-users" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to [email protected]. >>> >>> For more options, visit https://groups.google.com/d/optout. >>> >> >> -- > -- > v8-users mailing list > [email protected] > http://groups.google.com/group/v8-users > --- > You received this message because you are subscribed to the Google Groups > "v8-users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/d/optout. > -- -- v8-users mailing list [email protected] http://groups.google.com/group/v8-users --- You received this message because you are subscribed to the Google Groups "v8-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
