A simplified explanation of closures by Morris Johns: A closure is the local variables for a function - kept alive after the function has returned.
Cory Hudson sees callbacks as benefiting from currying JavaScript functions, and defines currying as turning a function with two arguments into a function with one argument that returns a function of one argument. He starts off with two functions and shows how to combine them into one curried function, and also builds a generic version. He shows a good usage of a curried function in an array map()
function, which is similar to Prototype's bind()
.
Jonathan Snook demonstrates when JavaScript passes by reference or passes by value. Essentially, primitive types are passed by value, objects are passed by reference. Passing functions however, makes things look like a pass by value if the this
keyword is being used in the code. Snook offers workarounds to this by passing objects so that the context is correct, or using the call()
function to ensure the context is correct.
Dan Webb describes curried functions as a way of creating reusable callback functions for event handlers or Ajax requests, or anything that takes a function as an argument. By using closures, curried functions have a simple way of persisting data between calls. He also offers an elegant way of running a lots of methods on objects, with a simple map function written as a curried function.
Douglas Crockford's JavaScript code conventions. Covers indentation, line length, comments, variable and function declarations, minification, statements and labels, whitespace, scope and eval.