I’ve been finding lately that my simplest blog posts grow the longest tails. So here is one for all you dojo toolkit users or wannabe’s. I started seeing the following in dojo scripts that I hadn’t seen before, and couldn’t find any specific comments about it.
;(function(d,$){ /* your code here */})(dojo, dojo.query)
The leading semicolon is a fudge in case you’re using the build tools and glomming multiple files and the one ahead of yours forgot its trailing semicolon. An extra semicolon won’t hurt anything.
In my world (linux conf files), a leading semicolon often is a comment marker, so even though I know that is not the case in ECMAscript (AKA Javascript), it still caused me to do a double take.
The purpose of the anonymous function is to parameterize dojo and dojo.query so that you can use d to refer to dojo, and the JQuery/prototype style $ as shorthand for queries. That’s all there is to it. It also serves to point up a less used way to both define and call a function all in one bang.
I’m not sure this merits inclusion in the toolkit since it makes assumptions about file locations, but works in pretty much all environments:
Well… if I found it needed and easy to use, I will add your code to use the options… BTW.. its a good move to create such a cool tool kit..