On 12/25/10, Yu-Hsuan Lai <[email protected]> wrote:
> Will closure create references to every object outer even when I don't
> explicitly use them?
>
> example:
> function outer () {
>     var div = document.createElement("div");
>     function inner() {
>           var a,b,c.... and do something blahblah without div;
>     }
>     outer = inner();
>     return inner;
> }
>
> In this case, I still have a memory leak?
>
Yes, you do. But what are you doing there?

You assigning `outer` to be the *result* of `inner`, and then return
`inner`, from `outer`. This will result in outer being called the
first time, returning the inner function. The second call to `outer`
will result in a call to the return value for inner. SInce inner
returns undefined, it means that `outer` is now holding value
`undefined`. Calling `undefined` MUST result in TypeError

For example, with eval:

 function outer(s) {
     var div = document.createElement("div");
     function inner() {
       // do something blahblah without div;
       return eval(s);
     }
     outer = inner("1");
     return inner;
 }

 function outer(s) {
     var div = document.createElement("div");
     function inner() {
       // do something without div;
       return eval(s);
     }
     outer = inner;
     return inner(s);
 }

Now what is going to happen in the call:

  outer('div');

?

Incidentally, that also answers the question about why eval is not safe.
[...]
-- 
Garrett

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to