Re:Why MooTools (or Why not JQuery)
by LogicHoleFlaw on Thursday October 02, 2008 @11:07AM
(#321768)
That's my preferred method of invoking jQuery. I get the convenient $() syntax without clobbering namespaces.
[ Parent
| on HN ]
Re:Why MooTools (or Why not JQuery)
by thomasmallen on Thursday October 02, 2008 @03:13PM
(#322032)
Sorry, but you're both wrong. Best practice is this:
(function($) {
// Plugin code
}) (jQuery)
And you'll see it in UI and most other good jQuery plugins.
Re:Why MooTools (or Why not JQuery)
by LogicHoleFlaw on Thursday October 02, 2008 @04:42PM
(#322132)
What is the advantage of (function($){})(jQuery) over jQuery(function($){}) ? In either case the jQuery object must be in the namespace, and neither leaves a named function laying about. According to the jQuery API jQuery(callback) is sugar for $(document).ready(callback). The ready event is usually when I want my code to run...
Re:Why MooTools (or Why not JQuery)
by humanchimp on Wednesday October 29, 2008 @10:02PM
(#348326)
The (function($){...})(jQuery) syntax is used by plug-in developers who want their code to run before the jQuery(document).ready event. It is closure, and it's purpose in this case is to protect the global namespace from variables introduced by the script. Closures also have many other useful applications in JavaScript.
This is one technically correct (which is usually the best kind of correct), but the jQuery() invocation has one advantage that I feel makes up for adding "jQuery" to the global namespace which is that the next person that needs to modify my javascript sees my code, they'll see the jQuery right up front and know what's up.
Re:Why MooTools (or Why not JQuery)
by thomasmallen on Thursday October 02, 2008 @07:01PM
(#322245)
That's what comments are for :^)
I always make the dependency pretty clear in my scripts. Plus, it's a "best practice" in the jQuery community to name your plugins in the form jquery.plugin.js or jquery.package.plugin.js.