Responding to Andre's blog about javascript closures.
From my understanding ....those are not just coding style. The one you mentioned
GDownloadUrl("data.xml", function(data, responseCode) { var xml = GXml.parse(data)
is passing an anonymous function as a parameter to a named function GDownloadUrl. Encapsulation is a powerful technique in object oriented programming and anonymous functions are one way to achieve this.
Lets say you have a series of functions that you don't want other users of your api to access. You can put them all together in one big anonymous function to encapsulate it. All your functions, objects and variables within the anonymous function will have access to each other, but not the ones outside of your anonymous function.
The other advantage is, you create a namesspace for all your functions and don't have to prefix your function with a namespace wherever its used ..to avoid naming collision.
Just my 2 cents ..hope this helps.
Recent Comments