最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Do nested function declarations create a new object each call? - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question edited May 24, 2014 at 2:02 Qix - MONICA WAS MISTREATED asked May 24, 2014 at 1:53 Qix - MONICA WAS MISTREATEDQix - MONICA WAS MISTREATED 15.2k17 gold badges92 silver badges156 bronze badges 3
  • 1 What does using .bind() have to do with not losing the closure values. I'm not understanding your point there. – cookie monster Commented May 24, 2014 at 2:25
  • Was just re-iterating that Foo(n).bind() wouldn't change n in the function returned by the call to bind. – Qix - MONICA WAS MISTREATED Commented May 24, 2014 at 2:29
  • @Downvoter, care to explain? – Qix - MONICA WAS MISTREATED Commented Jun 24, 2014 at 1:04
Add a ment  | 

3 Answers 3

Reset to default 5

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./forum/#!topic/v8-users/BbvL5qFG_uc

This code snippet shows that you're getting a new Function object each time:

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

var a = Foo(2);
var b = Foo(2);

alert(a === b);    // alerts false

Demo: http://jsfiddle/wc5Lv/


There are probably interpreter optimizations that can internally reuse the parsed function, but from the pure javascript point of view, a new Function is created each time.

Yes. The ECMAScript specification, 5ed, requires that each evaluation of a function expression or function declaration generates a new function object. If you read the cases of http://es5.github.io/#x13 they all contain the phrase "a new Function object"

That just means that there is a new Function object, but most of the internal content of that function object can be shared between instances, including the code generated for the function body. The new object only needs to hold the value of the product variable and a reference to the shared function implementation.

发布评论

评论列表(0)

  1. 暂无评论