views:

116

answers:

1

Hi,

While I was talking about javascript closure to my friend, I was told that using Mootools can prevent closures 100%. To my knowledege, a variable causes a closure. How does Mootools itself prevents javascript closure? I think my friend is saying that Mootools' functions are closure-safe functions.

Any suggestions?

A: 

A variable does not cause a closure. A closure is created by a function A that returns another function B referring to one of A's local variables. For example, the expression

  (function() {
    var x;
    return {
        get: function () { return x; },
        set: function (y) { return x=y; }
      };
  })();

returns an object containing two functions referring to the local variable x. We say that get and set "close over" x.

Mike Stay