Search code examples
javascriptv8

Do nested function declarations create a new object each call?


Does the anonymous function in Foo get re-created in memory each time Foo() gets called?

function Foo(product)
{
    return function(n) {
        return n * product;
    }
}

I'm more or less interested in V8's implementation in particular, since I'm not seeing anything in regards to that in the spec (unless I'm missing something, which I probably am).

I'm kind of confused on the memory management going on since the use of product is specific to the closure that is returned; however, that doesn't necessarily say the inner function has to be re-created along with a closure instance (in theory), since you can still .bind() without losing closure values.


Solution

  • As far as I know a new function object gets re-created everytime, but the function's code (body) is normally getting reused. I do not know under what circumstances it wouldn't be however.

    https://groups.google.com/forum/#!topic/v8-users/BbvL5qFG_uc