<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en-us" xmlns="http://www.w3.org/2005/Atom"><title>Simon Willison's Weblog: closures</title><link href="http://simonwillison.net/" rel="alternate"/><link href="http://simonwillison.net/tags/closures.atom" rel="self"/><id>http://simonwillison.net/</id><updated>2013-07-30T12:09:00+00:00</updated><author><name>Simon Willison</name></author><entry><title>What are some JavaScript concepts that took you the longest to understand?</title><link href="https://simonwillison.net/2013/Jul/30/what-are-some-javascript/#atom-tag" rel="alternate"/><published>2013-07-30T12:09:00+00:00</published><updated>2013-07-30T12:09:00+00:00</updated><id>https://simonwillison.net/2013/Jul/30/what-are-some-javascript/#atom-tag</id><summary type="html">
    &lt;p&gt;&lt;em&gt;My answer to &lt;a href="https://www.quora.com/What-are-some-JavaScript-concepts-that-took-you-the-longest-to-understand/answer/Simon-Willison"&gt;What are some JavaScript concepts that took you the longest to understand?&lt;/a&gt; on Quora&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Closures, prototype inheritance, and the "this" keyword.&lt;/p&gt;

&lt;p&gt;Once you understand those three, you know most of what there is to know about the JavaScript language in my opinion (with the exception of some of the newer language features like iterators and generators).&lt;/p&gt;
    
        &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/closures"&gt;closures&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/javascript"&gt;javascript&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/quora"&gt;quora&lt;/a&gt;&lt;/p&gt;
    

</summary><category term="closures"/><category term="javascript"/><category term="quora"/></entry><entry><title>10 Uses for Blocks in C/Objective-C</title><link href="https://simonwillison.net/2009/Nov/16/cocoa/#atom-tag" rel="alternate"/><published>2009-11-16T14:27:15+00:00</published><updated>2009-11-16T14:27:15+00:00</updated><id>https://simonwillison.net/2009/Nov/16/cocoa/#atom-tag</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="http://www.macresearch.org/cocoa-scientists-xxxii-10-uses-blocks-cobjective-c"&gt;10 Uses for Blocks in C/Objective-C&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
Part of the Cocoa for Scientists series, which is by far the best free Objective-C / Cocoa tutorial I’ve seen anywhere.


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/blocks"&gt;blocks&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/closures"&gt;closures&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/cocoa"&gt;cocoa&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/macos"&gt;macos&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/objective-c"&gt;objective-c&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/science"&gt;science&lt;/a&gt;&lt;/p&gt;



</summary><category term="blocks"/><category term="closures"/><category term="cocoa"/><category term="macos"/><category term="objective-c"/><category term="science"/></entry><entry><title>And so it goes, around again</title><link href="https://simonwillison.net/2009/Sep/3/and/#atom-tag" rel="alternate"/><published>2009-09-03T09:46:33+00:00</published><updated>2009-09-03T09:46:33+00:00</updated><id>https://simonwillison.net/2009/Sep/3/and/#atom-tag</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="http://fishbowl.pastiche.org/2009/09/02/and_so_it_goes_around_again/"&gt;And so it goes, around again&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
Charles Miller on Java, pointing out that if you don’t have closures and first-class functions you end up having to add band-aid solutions and special case syntactic sugar. Python’s lack of multi-line lambdas leads to a similar (though less pronounced) effect.


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/charles-miller"&gt;charles-miller&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/closures"&gt;closures&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/java"&gt;java&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/programming-languages"&gt;programming-languages&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/python"&gt;python&lt;/a&gt;&lt;/p&gt;



</summary><category term="charles-miller"/><category term="closures"/><category term="java"/><category term="programming-languages"/><category term="python"/></entry><entry><title>Mac OS X 10.6 Snow Leopard: the Ars Technica review</title><link href="https://simonwillison.net/2009/Sep/1/siracusa/#atom-tag" rel="alternate"/><published>2009-09-01T19:05:25+00:00</published><updated>2009-09-01T19:05:25+00:00</updated><id>https://simonwillison.net/2009/Sep/1/siracusa/#atom-tag</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="http://arstechnica.com/apple/reviews/2009/08/mac-os-x-10-6.ars"&gt;Mac OS X 10.6 Snow Leopard: the Ars Technica review&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
The essential review: 23 pages of information-dense but readable goodness. Pretty much everything I know about Mac OS X internals I learnt from reading John Siracusa’s reviews—this one is particularly juice when it gets to Grand Central Dispatch and blocks (aka closures) in C and Objective-C.


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/apple"&gt;apple&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/blocks"&gt;blocks&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/c"&gt;c&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/closures"&gt;closures&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/grandcentraldispatch"&gt;grandcentraldispatch&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/john-siracusa"&gt;john-siracusa&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/macos"&gt;macos&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/objective-c"&gt;objective-c&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/snowleopard"&gt;snowleopard&lt;/a&gt;&lt;/p&gt;



</summary><category term="apple"/><category term="blocks"/><category term="c"/><category term="closures"/><category term="grandcentraldispatch"/><category term="john-siracusa"/><category term="macos"/><category term="objective-c"/><category term="snowleopard"/></entry><entry><title>Why I don't love JavaScript's Module Pattern</title><link href="https://simonwillison.net/2009/Apr/30/module/#atom-tag" rel="alternate"/><published>2009-04-30T19:59:39+00:00</published><updated>2009-04-30T19:59:39+00:00</updated><id>https://simonwillison.net/2009/Apr/30/module/#atom-tag</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="http://snook.ca/archives/javascript/no-love-for-module-pattern"&gt;Why I don&amp;#x27;t love JavaScript&amp;#x27;s Module Pattern&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
Jonathan Snook points out that the module pattern (where private functionality is hidden in a closure and only public methods are revealed to outside scopes) makes code a lot harder to debug. I use the module pattern for pretty much everything, not because I want to keep stuff private but more to avoid any chance of leaking out in to the global namespace. If I need to debug a value I temporarily assign it as a property on the global window object.


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/closures"&gt;closures&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/debugging"&gt;debugging&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/javascript"&gt;javascript&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/jonathan-snook"&gt;jonathan-snook&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/modulepattern"&gt;modulepattern&lt;/a&gt;&lt;/p&gt;



</summary><category term="closures"/><category term="debugging"/><category term="javascript"/><category term="jonathan-snook"/><category term="modulepattern"/></entry><entry><title>why's potion</title><link href="https://simonwillison.net/2009/Jan/8/whys/#atom-tag" rel="alternate"/><published>2009-01-08T18:37:34+00:00</published><updated>2009-01-08T18:37:34+00:00</updated><id>https://simonwillison.net/2009/Jan/8/whys/#atom-tag</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="http://github.com/why/potion/tree/master"&gt;why&amp;#x27;s potion&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
why’s latest project is a small, fast language (JIT to x86/x86-64) which seems to take ideas from Ruby, Lua, Python and who knows where else. Everything is based around objects, closures and mixins, with the delightful inclusion of scoped mixins so you can modify an object only within a certain module (hence avoiding Ruby’s action-at-a-distance problems).


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/closures"&gt;closures&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/jit"&gt;jit&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/languages"&gt;languages&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/lua"&gt;lua&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/mixins"&gt;mixins&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/potion"&gt;potion&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/programming"&gt;programming&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/ruby"&gt;ruby&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/whytheluckystiff"&gt;whytheluckystiff&lt;/a&gt;&lt;/p&gt;



</summary><category term="closures"/><category term="jit"/><category term="languages"/><category term="lua"/><category term="mixins"/><category term="potion"/><category term="programming"/><category term="ruby"/><category term="whytheluckystiff"/></entry><entry><title>Blocks in Objective-C</title><link href="https://simonwillison.net/2008/Dec/29/blocks/#atom-tag" rel="alternate"/><published>2008-12-29T19:38:08+00:00</published><updated>2008-12-29T19:38:08+00:00</updated><id>https://simonwillison.net/2008/Dec/29/blocks/#atom-tag</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="http://www.mikeash.com/?page=pyblog/friday-qa-2008-12-26.html"&gt;Blocks in Objective-C&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
Closures are coming soon to Objective-C - interesting syntax, a regular curly brace block preceded by a caret &lt;code&gt;^{ ... }&lt;/code&gt;.


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/blocks"&gt;blocks&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/closures"&gt;closures&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/objective-c"&gt;objective-c&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/programming"&gt;programming&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/syntax"&gt;syntax&lt;/a&gt;&lt;/p&gt;



</summary><category term="blocks"/><category term="closures"/><category term="objective-c"/><category term="programming"/><category term="syntax"/></entry><entry><title>php: rfc: closures</title><link href="https://simonwillison.net/2008/Jul/19/closures/#atom-tag" rel="alternate"/><published>2008-07-19T22:58:45+00:00</published><updated>2008-07-19T22:58:45+00:00</updated><id>https://simonwillison.net/2008/Jul/19/closures/#atom-tag</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="http://wiki.php.net/rfc/closures"&gt;php: rfc: closures&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
I never thought I’d see the day, but a patch adding closures to PHP has been both proposed and accepted! Looks like a solid implementation—the syntax is similar to JavaScript but makes explicit which variables are to be captured. As with much of PHP, values are copied in to the closure by default but you can use an ampersand to specify JavaScript-style pass-by-reference instead.

    &lt;p&gt;&lt;small&gt;&lt;/small&gt;Via &lt;a href="http://www.sitepoint.com/blogs/2008/07/16/keeping-current-with-php/"&gt;Keeping Current With PHP&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/closures"&gt;closures&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/functional-programming"&gt;functional-programming&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/javascript"&gt;javascript&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/php"&gt;php&lt;/a&gt;&lt;/p&gt;



</summary><category term="closures"/><category term="functional-programming"/><category term="javascript"/><category term="php"/></entry><entry><title>Module Pattern Provides No Privacy... at least not in JavaScript(TM)</title><link href="https://simonwillison.net/2008/Jun/27/peters/#atom-tag" rel="alternate"/><published>2008-06-27T19:01:34+00:00</published><updated>2008-06-27T19:01:34+00:00</updated><id>https://simonwillison.net/2008/Jun/27/peters/#atom-tag</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="http://peter.michaux.ca/article/8069"&gt;Module Pattern Provides No Privacy... at least not in JavaScript(TM)&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
JavaScript variables hidden inside a closure aren’t as hidden as I thought—it turns out you can pass a closure as the second argument to eval (at least in Firefox) and “steal” private variables back out of it.

    &lt;p&gt;&lt;small&gt;&lt;/small&gt;Via &lt;a href="http://ajaxian.com/archives/evalfooa-objfn-how-you-arent-private-in-firefox"&gt;Ajaxian&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/closures"&gt;closures&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/firefox"&gt;firefox&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/javascript"&gt;javascript&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/modulepattern"&gt;modulepattern&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/pete-michaux"&gt;pete-michaux&lt;/a&gt;&lt;/p&gt;



</summary><category term="closures"/><category term="firefox"/><category term="javascript"/><category term="modulepattern"/><category term="pete-michaux"/></entry><entry><title>IE JScript Performance Recommendations Part 3</title><link href="https://simonwillison.net/2007/Jan/9/ieblog/#atom-tag" rel="alternate"/><published>2007-01-09T11:48:10+00:00</published><updated>2007-01-09T11:48:10+00:00</updated><id>https://simonwillison.net/2007/Jan/9/ieblog/#atom-tag</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="http://blogs.msdn.com/ie/archive/2007/01/04/ie-jscript-performance-recommendations-part-3-javascript-code-inefficiencies.aspx"&gt;IE JScript Performance Recommendations Part 3&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
Once again, Microsoft’s official advice is to avoid closures entirely rather than learn how to use them safely. Sigh.


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/closures"&gt;closures&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/internet-explorer"&gt;internet-explorer&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/javascript"&gt;javascript&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/microsoft"&gt;microsoft&lt;/a&gt;&lt;/p&gt;



</summary><category term="closures"/><category term="internet-explorer"/><category term="javascript"/><category term="microsoft"/></entry><entry><title>JavaScript Closures for Dummies</title><link href="https://simonwillison.net/2006/Jul/6/javascript/#atom-tag" rel="alternate"/><published>2006-07-06T10:01:03+00:00</published><updated>2006-07-06T10:01:03+00:00</updated><id>https://simonwillison.net/2006/Jul/6/javascript/#atom-tag</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="http://blog.morrisjohns.com/javascript_closures_for_dummies"&gt;JavaScript Closures for Dummies&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
Not quite for Dummies as it talks about C pointers, but a valiant attempt.


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/closures"&gt;closures&lt;/a&gt;&lt;/p&gt;



</summary><category term="closures"/></entry><entry><title>Closures And Highly Readable Sequence Sorting Customization</title><link href="https://simonwillison.net/2006/Jan/24/closures/#atom-tag" rel="alternate"/><published>2006-01-24T00:23:08+00:00</published><updated>2006-01-24T00:23:08+00:00</updated><id>https://simonwillison.net/2006/Jan/24/closures/#atom-tag</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/466335"&gt;Closures And Highly Readable Sequence Sorting Customization&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
Really neat usage of closures in Python.


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/closures"&gt;closures&lt;/a&gt;&lt;/p&gt;



</summary><category term="closures"/></entry><entry><title>Getting Funky With Scopes and Closures</title><link href="https://simonwillison.net/2006/Jan/23/getting/#atom-tag" rel="alternate"/><published>2006-01-23T10:27:30+00:00</published><updated>2006-01-23T10:27:30+00:00</updated><id>https://simonwillison.net/2006/Jan/23/getting/#atom-tag</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="http://novemberborn.net/javascript/scopes-and-closures-funk"&gt;Getting Funky With Scopes and Closures&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
Mark Wubben has some neat tricks up his sleeve.


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/closures"&gt;closures&lt;/a&gt;&lt;/p&gt;



</summary><category term="closures"/></entry><entry><title>Leak Free Javascript Closures</title><link href="https://simonwillison.net/2005/Oct/27/leak/#atom-tag" rel="alternate"/><published>2005-10-27T13:52:25+00:00</published><updated>2005-10-27T13:52:25+00:00</updated><id>https://simonwillison.net/2005/Oct/27/leak/#atom-tag</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="http://laurens.vd.oever.nl/weblog/items2005/closures/"&gt;Leak Free Javascript Closures&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
This is some pretty brilliant JavaScript voodoo.


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/closures"&gt;closures&lt;/a&gt;&lt;/p&gt;



</summary><category term="closures"/></entry><entry><title>Another practical use for JavaScript closures</title><link href="https://simonwillison.net/2005/Jul/31/another/#atom-tag" rel="alternate"/><published>2005-07-31T13:08:59+00:00</published><updated>2005-07-31T13:08:59+00:00</updated><id>https://simonwillison.net/2005/Jul/31/another/#atom-tag</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="http://www.davidflanagan.com/blog/2005_07.html#000071"&gt;Another practical use for JavaScript closures&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
Create private methods by hiding them in a closure.


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/closures"&gt;closures&lt;/a&gt;&lt;/p&gt;



</summary><category term="closures"/></entry><entry><title>Martin Fowler on Closures</title><link href="https://simonwillison.net/2004/Sep/9/martin/#atom-tag" rel="alternate"/><published>2004-09-09T07:51:10+00:00</published><updated>2004-09-09T07:51:10+00:00</updated><id>https://simonwillison.net/2004/Sep/9/martin/#atom-tag</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="http://martinfowler.com/bliki/Closures.html"&gt;Martin Fowler on Closures&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
They really aren’t that complicated once they’re explained well.


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/closures"&gt;closures&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/martin-fowler"&gt;martin-fowler&lt;/a&gt;&lt;/p&gt;



</summary><category term="closures"/><category term="martin-fowler"/></entry><entry><title>XMLHttpRequest and Javascript Closures</title><link href="https://simonwillison.net/2004/May/27/xmlhttprequest/#atom-tag" rel="alternate"/><published>2004-05-27T00:23:03+00:00</published><updated>2004-05-27T00:23:03+00:00</updated><id>https://simonwillison.net/2004/May/27/xmlhttprequest/#atom-tag</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="http://www.sitepoint.com/blog-post-view.php?id=171725"&gt;XMLHttpRequest and Javascript Closures&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
Harry gets intimate with Mozilla’s XMLHttpRequest object.


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/closures"&gt;closures&lt;/a&gt;&lt;/p&gt;



</summary><category term="closures"/></entry><entry><title>Javascript Closures</title><link href="https://simonwillison.net/2004/May/26/javascript/#atom-tag" rel="alternate"/><published>2004-05-26T16:52:21+00:00</published><updated>2004-05-26T16:52:21+00:00</updated><id>https://simonwillison.net/2004/May/26/javascript/#atom-tag</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="http://jibbering.com/faq/faq_notes/closures.html"&gt;Javascript Closures&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
Notes on Closures.


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/closures"&gt;closures&lt;/a&gt;&lt;/p&gt;



</summary><category term="closures"/></entry><entry><title>Closures and executing JavaScript on page load</title><link href="https://simonwillison.net/2004/May/26/closures/#atom-tag" rel="alternate"/><published>2004-05-26T07:09:29+00:00</published><updated>2004-05-26T07:09:29+00:00</updated><id>https://simonwillison.net/2004/May/26/closures/#atom-tag</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="http://www.sitepoint.com/blog-post-view.php?id=171578"&gt;Closures and executing JavaScript on page load&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
I’ve tried to explain closures on my SitePoint blog, using addLoadEvent() as an example.


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/closures"&gt;closures&lt;/a&gt;&lt;/p&gt;



</summary><category term="closures"/></entry><entry><title>Executing JavaScript on page load</title><link href="https://simonwillison.net/2004/May/26/addLoadEvent/#atom-tag" rel="alternate"/><published>2004-05-26T06:30:37+00:00</published><updated>2004-05-26T06:30:37+00:00</updated><id>https://simonwillison.net/2004/May/26/addLoadEvent/#atom-tag</id><summary type="html">
    &lt;p id="p-0"&gt;Peter-Paul Koch &lt;a href="http://www.digital-web.com/articles/separating_behavior_and_structure_2/" title="Separating behavior and structure"&gt;recently wrote&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote cite="http://www.digital-web.com/articles/separating_behavior_and_structure_2/"&gt;&lt;p id="p-1"&gt;In my opinion, recent advances in JavaScript theory call for the removal of the event handlers that some Web developers-and all WYSIWYG editors-deploy in large masses in their XHTML files, where they don’t belong.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p id="p-2"&gt;&lt;acronym title="Peter-Paul Koch"&gt;PPK&lt;/acronym&gt; is talking about inline event attributes such as the infamous &lt;code class="html"&gt;onclick=""&lt;/code&gt; and &lt;code class="html"&gt;onmouseover=""&lt;/code&gt; which have infested our &lt;acronym title="HyperText Markup Language"&gt;HTML&lt;/acronym&gt; ever since Netscape introduced JavaScript back in version 2.0 of their browser. The alternative to these handlers is to add event handlers to elements after the document has loaded. &lt;acronym title="Peter-Paul Koch"&gt;PPK&lt;/acronym&gt; has detailed coverage of the various ways of doing this &lt;a href="http://www.quirksmode.org/js/introevents.html" title="Introduction to Events"&gt;on his QuirksMode site&lt;/a&gt;.&lt;/p&gt;

&lt;p id="p-3"&gt;In my work with unobtrusive JavaScript, I've found that by far the most common action I take is "registering" a script to be executed once the page has finished loading. There are a number of ways of doing this, which I described in my article &lt;a href="http://www.sitepoint.com/article/structural-markup-javascript"&gt;Enhancing Structural Markup with JavaScript&lt;/a&gt;. Unfortunately, none of them are perfect if you wish to write truly reusable scripts.&lt;/p&gt;

&lt;p id="p-4"&gt;For a script (such as my blockquote citations script discussed in the article) to be properly reusable, it needs to behave nicely in the presence of other scripts. This means that assigning a callback function directly to the &lt;code class="javascript"&gt;window.onload&lt;/code&gt; handler is out of the question as doing so will over-ride previously assigned callbacks from other scripts. The correct way of adding a handler to an event without over-riding other handlers is to use modern event attachment method, which sadly differ between &lt;acronym title="Internet Explorer"&gt;IE&lt;/acronym&gt;/Windows and other browsers. Scott Andrew's &lt;a href="http://www.scottandrew.com/weblog/jsjunk#events" title="addEvent / removeEvent"&gt;addEvent function&lt;/a&gt; handles the differences for you but comes with one major and rarely discussed drawback: it fails silently in &lt;acronym title="Internet Explorer"&gt;IE&lt;/acronym&gt;5/Mac. If you care about the many Mac users still on OS9, you need to support that browser.&lt;/p&gt;

&lt;p id="p-5"&gt;Anyway, I believe I've found a solution. Check this out:&lt;/p&gt;

&lt;pre&gt;&lt;code class="javascript"&gt;
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent(nameOfSomeFunctionToRunOnPageLoad);
addLoadEvent(function() {
  /* more code to run on page load */ 
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p id="p-6"&gt;The &lt;code class="javascript"&gt;addLoadEvent&lt;/code&gt; function takes as an argument another function which should be executed once the page has loaded. Unlike assigning directly to &lt;code class="javascript"&gt;window.onload&lt;/code&gt;, the function adds the event in such a way that any previously added onload functions will be executed first.&lt;/p&gt;

&lt;p id="p-7"&gt;The way this works is relatively simple: if &lt;code class="javascript"&gt;window.onload&lt;/code&gt; has not already been assigned a function, the function passed to &lt;code class="javascript"&gt;addLoadEvent&lt;/code&gt; is simply assigned to &lt;code class="javascript"&gt;window.onload&lt;/code&gt;. If &lt;code class="javascript"&gt;window.onload&lt;/code&gt; has already been set, a brand new function is created which first calls the original onload handler, then calls the new handler afterwards.&lt;/p&gt;

&lt;p id="p-8"&gt;&lt;code class="javascript"&gt;addLoadEvent&lt;/code&gt; has one very important property: it will work even if something has previously been assigned to window.onload without using &lt;code class="javascript"&gt;addLoadEvent&lt;/code&gt; itself. This makes it ideal for use in scripts that may be executing along side other scripts that have already been registered to execute once the page has loaded.&lt;/p&gt;

&lt;p id="p-9"&gt;I've tested the above code successfully on &lt;acronym title="Internet Explorer"&gt;IE&lt;/acronym&gt; 5, 5.5 and 6 for Windows; &lt;acronym title="Internet Explorer"&gt;IE&lt;/acronym&gt; 5 and Safari for Mac; Opera 7.5 and FireFox on Mac (which should mean it works with those browsers on Windows as well). Opera 6 for Mac failed the test but has poor JavaScript support anyway and is hopefully becoming more and more rare now that Opera 7 has matured.&lt;/p&gt;

&lt;p id="p-10"&gt;I've created &lt;a href="http://simon.incutio.com/code/js/addloadevent/" title="addLoadEvent() test"&gt;a test page&lt;/a&gt; for the function. I'd be interested to here any bug reports from browsers I haven't covered.&lt;/p&gt;

&lt;p id="p-11"&gt;I'm still considering ways in which this technique could be extended to work for generic events rather than just page loads. The challenge there would be to ensure that information about the event itself was passed to the event handlers in a consistent manner. For page load events this isn't an issue as the event object does not contain any valuable information.&lt;/p&gt;

&lt;p id="p-12"&gt;&lt;strong&gt;Update:&lt;/strong&gt; I've written the new technique up &lt;a href="http://www.sitepoint.com/blog-post-view.php?id=171578" title="Closures and executing JavaScript on page load"&gt;on my SitePoint blog&lt;/a&gt; and incorporated an explanation of closures and how they are used to preserve any previously assigned onload handlers.&lt;/p&gt;

&lt;p id="p-13"&gt;&lt;strong&gt;Update 28th May 2006&lt;/strong&gt;: Billy Pan pointed out that the original code caused a runtime error in IE 7. His fix was to add an &lt;code&gt;if (oldonload)&lt;/code&gt;; I have rolled this fix in to the code shown above.&lt;/p&gt;
    
        &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/addloadevent"&gt;addloadevent&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/closures"&gt;closures&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/javascript"&gt;javascript&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/onload"&gt;onload&lt;/a&gt;, &lt;a href="https://simonwillison.net/tags/unobtrusive-javascript"&gt;unobtrusive-javascript&lt;/a&gt;&lt;/p&gt;
    

</summary><category term="addloadevent"/><category term="closures"/><category term="javascript"/><category term="onload"/><category term="unobtrusive-javascript"/></entry><entry><title>Groovy: Closures</title><link href="https://simonwillison.net/2004/Apr/11/groovy/#atom-tag" rel="alternate"/><published>2004-04-11T08:01:16+00:00</published><updated>2004-04-11T08:01:16+00:00</updated><id>https://simonwillison.net/2004/Apr/11/groovy/#atom-tag</id><summary type="html">
    
&lt;p&gt;&lt;strong&gt;&lt;a href="http://groovy.codehaus.org/closures.html"&gt;Groovy: Closures&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;
I’d nearly written Groovy off as a pointless Jython clone, but it actually has some interesting features.


    &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/closures"&gt;closures&lt;/a&gt;&lt;/p&gt;



</summary><category term="closures"/></entry><entry><title>Closures and continuations</title><link href="https://simonwillison.net/2003/Apr/3/closuresAndContinuations/#atom-tag" rel="alternate"/><published>2003-04-03T18:56:07+00:00</published><updated>2003-04-03T18:56:07+00:00</updated><id>https://simonwillison.net/2003/Apr/3/closuresAndContinuations/#atom-tag</id><summary type="html">
    &lt;p&gt;Thanks to Dan Sugalski (designer of &lt;a href="http://www.parrotcode.org/"&gt;Parrot&lt;/a&gt;, the next generation Perl &lt;acronym title="Virtual Machine"&gt;VM&lt;/acronym&gt;) I finally understand what continuations and closures actually are. He explains them as part of a comparison between the forthcoming Parrot and two popular virtual machines already in existence:&lt;/p&gt;

&lt;ol&gt;
 &lt;li&gt;&lt;a href="http://www.sidhe.org/~dan/blog/archives/000151.html"&gt;(Perl|python|Ruby) on (.NET|JVM)&lt;/a&gt; leads in to the explanation.&lt;/li&gt;
 &lt;li&gt;&lt;a href="http://www.sidhe.org/~dan/blog/archives/000152.html"&gt;The reason for Parrot, part 2&lt;/a&gt; explains closures.&lt;/li&gt;
 &lt;li&gt;&lt;a href="http://www.sidhe.org/~dan/blog/archives/000156.html"&gt;Continuations and VMs&lt;/a&gt; explains continuations.&lt;/li&gt;
 &lt;li&gt;&lt;a href="http://www.sidhe.org/~dan/blog/archives/000157.html"&gt;Continuations and VMs, part 2&lt;/a&gt; rounds things off by explaining why the &lt;acronym title="Java Virtual Machine"&gt;JVM&lt;/acronym&gt; and the &lt;acronym title="Common Language Runtime"&gt;CLR&lt;/acronym&gt; are unsuitable environments for supporting these language features.&lt;/li&gt;
&lt;/ol&gt;
    
        &lt;p&gt;Tags: &lt;a href="https://simonwillison.net/tags/closures"&gt;closures&lt;/a&gt;&lt;/p&gt;
    

</summary><category term="closures"/></entry></feed>