On 12/25/10, Garrett Smith <dhtmlkitc...@gmail.com> wrote: > On 12/25/10, Yu-Hsuan Lai <rainco...@gmail.com> 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: > [snip code] I should have omitted that first function. I copy'n'pasted Yu's example and then noticed a problem and fixed it, then decided to mention about the odd problem but did not delete it.
I accidentally sent the email message with a combination of keystrokes. I don't know what those keystrokes were; I was typing and then all of a sudden, the page in gmail loaded. I may have tabbed to send, or I'm not sure what I did. But this example was the right one. > > function outer(s) { > var div = document.createElement("div"); > function inner() { > // do something without div; > return eval(s); > } > outer = inner; > return inner(s); > } > And the question that follows it -- and really everything else in my msg looks right. -- Garrett -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/jsmentors@jsmentors.com/ To search via a non-Google archive, visit here: http://www.mail-archive.com/jsmentors@googlegroups.com/ To unsubscribe from this group, send email to jsmentors+unsubscr...@googlegroups.com