0

I'm learning about closures and I want to know a bit about the garbage collector behavior. In the following code:

function valueMaker(name, value) {
    var final = {};
    final[name] = value;
    final[double] = value*2;
    return final;
}
first = valueMaker('first', 1);

When I call valueMaker it creates a closure where I have the final object, then, it returns the object.

My question is: The returned object count as a reference to the closure? or JavaScript is smart enough to know when I want to keep the closure alive?

lcjury
  • 1,158
  • 1
  • 14
  • 26
  • 2
    I don't see a closure, here. – NineBerry Feb 11 '17 at 22:18
  • @NineBerry: When you execute a function, there is always a closure associate to the function execution: http://www.ecma-international.org/ecma-262/6.0/#sec-function-definitions-runtime-semantics-evaluation – lcjury Feb 11 '17 at 22:31
  • 1
    No, closures are bound to the creation of functions, not the execution of functions. When a function is created, it can bind to the local variables of an outer function wherein the inner function is created. This is then called a closure. We don't see this here, because valueMaker does not access any outside variables and there is no other function created inside valueMaker. – NineBerry Feb 11 '17 at 22:49
  • @NineBerry I would be awesome if you provide a reference to prove that. Everything I find makes me think you're wrong. From mozilla developers: "A closure is the combination of a function and the lexical environment within which that function was declared. This environment consists of any local variables that were in-scope at the time that the closure was created." [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures] closure = function + lexical environment. – lcjury Feb 11 '17 at 22:59
  • 1
    The Mozilla doc says the same as I did. You just have to look at the examples. There is no case of a closure in your code. – NineBerry Feb 11 '17 at 23:04

1 Answers1

2

When I call valueMaker it creates a closure

No. There is no function inside valueMaker, so there's no closure anywhere.

A closure was created when the function valueMaker definition itself was evaluated, and that closure will be used when you call valueMaker(). Though it's not even an interesting closure, given that there are no free variables that it would use.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • "personally I say No because they don't reference an outer scope.". I will never understand that urge to put the personal opinion behind what the truly happens (in this case, the compiler does). – lcjury Feb 12 '17 at 19:39
  • @lcjury However, the spec says something different. I wanted to emphasize that it depends on the view, there are arguments both for and against calling a function without free variables a closure - in any case, those cases aren't interesting. To be honest, I didn't remember that I said No two years ago, and here I'm making the opposite point. – Bergi Feb 12 '17 at 22:01
  • Completely agree with you, but I wanted to know the compiler point of view. Not the programmer point of view. I'm a little tired of this, each month I have a "strange" question, and when I ask other programmers about it all I got is "why do you want to know that?, anybody cares... better use this and ignore that...". – lcjury Feb 12 '17 at 23:08