0

Refering to How do JavaScript closures work?.

Closure is:

  • a closure is the local variables for a function — kept alive after the function has returned, or

  • a closure is a stack-frame which is not deallocated when the function returns (as if a 'stack-frame' were malloc'ed instead of being on the stack!).

Just want to confirm are the following consider closure?

1) Binding javascript function within a function.

var Books = {
    init:function(){
        $('#box').bind('click',function(){
           console.log('click'); 
        });
    }
};

Books.init();​

2) Declare a function within a function

function sayHello(name) {
  var text = 'Hello ' + name;
  var sayAlert = function() { alert(text); }
}   

var hello = sayHello();

I still can't differentiate which is closure for some times, is that all function within function consider closure or only the one that keep the returned inner function as variable/reference. Example:

function sayHello(name) {
  var text = 'Hello ' + name;
  var sayAlert = function() { alert(text); }
  **return sayAlert;**
}
Community
  • 1
  • 1
TonyTakeshi
  • 5,869
  • 10
  • 51
  • 72
  • Quite sure `var sayAlert = function() { alert(text); }` makes the number 2 closure, since it accesses `text` from outside. – nhahtdh Jul 13 '12 at 09:01
  • Sorry to break it to you but "as if a 'stack-frame' were malloc'ed instead of being on the stack!" - well... yes it is malloc'ed... because it's an interpreted language. Java, .Net, JavaScript, PHP all of their functions are in the heap. – Mihai Stancu Jul 13 '12 at 09:04

2 Answers2

1

1 isn't as no variables are actually referenced, 2 and 3 are.

In 2 and 3 the variable called text is closed off - referenced outside its lexical scope. In 1 nothing is.

crockford on closures :

What this means is that an inner function always has access to the vars and parameters of its outer function, even after the outer function has returned.

NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
  • 2
    1 has two functions. The outer one is not a closure, but the inner one is. (It doesn't do anything with the variables, but javascript still maintains them.) – Ariel Jul 13 '12 at 09:02
  • @Ariel exactly it doesn't do anythign with the variables. You could put one there, I agree. But there isn't. – NimChimpsky Jul 13 '12 at 09:04
  • And yet, it's still a closure, just a blank one. You can manipulate a function from outside it (change the source code), and if you did you would find that it maintains all the variables, even though none are referenced. – Ariel Jul 13 '12 at 09:06
0

All of those are closures.

I'm not sure what your confusion is.

Ariel
  • 25,995
  • 5
  • 59
  • 69
  • 1
    what variable is closed off in 1 ? You could put a variable in the init function and reference it in the inner function, that would be a closure, but there isn't. – NimChimpsky Jul 13 '12 at 09:02
  • 2
    @NimChimpsky No variable at all. But it's still a closure. See the jQuery `noop()` function - they created fixed function instead of defining it as a blank function each time because the closure slowed down the browser - despite having no variables in it. – Ariel Jul 13 '12 at 09:03
  • @Ariel I agree you could create a closure, but thats bit like saying every class has an inner class in java - they could, but they don't. – NimChimpsky Jul 13 '12 at 09:06
  • 1
    @NimChimpsky That's not how javascript works. All the variables are maintained in memory by the javascript engine, so that makes it a closure. You might not be referencing any, but it doesn't matter - the engine still stores them. – Ariel Jul 13 '12 at 09:08
  • @Ariel just having a function inside a function is not a closure – NimChimpsky Jul 13 '12 at 09:10
  • 1
    @NimChimpsky The thing that makes a function a closure is not *using* the outer variables - it's *storing* the variables. And since javascript always stores them, it's a closure. – Ariel Jul 13 '12 at 09:11
  • 1
    @NimChimpsky @http://jibbering.com/faq/notes/closures/ `The simple explanation of a Closure is that ECMAScript allows inner functions; function definitions and function expressions that are inside the function bodes of other functions. And that those inner functions are **allowed** access to all of the local variables, parameters and declared inner functions within their outer function(s). A closure is formed when one of those inner functions is made accessible outside of the function in which it was contained, so that it may be executed after the outer function has returned.` –  Jul 13 '12 at 09:12
  • @Ariel from that you think any nested function is a closure in javascript ? Thats not the case. – NimChimpsky Jul 13 '12 at 09:14
  • +, first sentence @ http://jibbering.com/faq/notes/closures/#clAc : `Rendering any inner function accessible outside of the body of the function in which it was created will form a closure.` - and the example is quite nice ... the outer-function from example 1 is accessing jquery (so a global access to a variable) and stores the function (inner one) to the handler ... well it's argueble if `'click'` is a global variable... with some precompiled javascript (like chrome), it might get stored in a global variable ... –  Jul 13 '12 at 09:20
  • @AndreasNiedermair well I disagree with that, for the same reason. Just having an inner function is not a closure. – NimChimpsky Jul 13 '12 at 09:20
  • @NimChimpsky you might read my comment i've added while you wrote yours :) –  Jul 13 '12 at 09:21
  • @AndreasNiedermair the example references a variable outside of the function, the question here doesn't. – NimChimpsky Jul 13 '12 at 09:23
  • 1
    @NimChimpsky that's not the point ... `And that those inner functions are **allowed** access to all of the local variables, parameters and declared inner functions within their outer function(s).` see @ same page. also, as i've edited: well it's argueble if 'click' is a global variable... with some precompiled javascript (like chrome), it might get stored in a global variable ... –  Jul 13 '12 at 09:24
  • 1
    @NimChimpsky Yes, any nested function is a closure in javascript. Just because you don't like it doesn't make it not true. – Ariel Jul 13 '12 at 09:24
  • @Ariel http://images.cheezburger.com/completestore/2010/12/6/5fa02f7f-a16a-49a8-bf3b-023566e94ed3.jpg :) –  Jul 13 '12 at 09:27