Don’t be afraid to use any technical jargon or low-level explanations for things, please. I’m savvy enough with computer architecture and low-level programming languages to comprehend any optimizations or memory management techniques, as well as complex structures (classes, member variables, etc.)
My primary focus of code is web-based applications. I work with PHP a lot and I’ve been learning CSS quickly. Javascript is currently my bottleneck, however. I know enough Javascript to do just about anything sans frameworks (DOM manipulation, AJAX queries, etc.). I also know that I can make my code run quicker, optimize it for specific cases, and I can shrink the over-all size of my code (no external script to include) by manually coding everything. However for ease of reading by other programmers and for speed of coding I’m trying to learn at least one Javascript framework.
After reading through the documentation on a number of frameworks and looking at some tutorials, I preferred jQuery. It allowed for very powerful iterative code in a single line and it had a very small chance of global variable namespace collision. From what I could tell, the ONLY global variable declared is the $ variable and everything else happens within this namespace, and there were even ways to access the namespace without this variable if you wanted to have two frameworks side-by-side. It also had a very small file to include (24 kilobytes gzipped) which means less server load.
My question is what are good practices in creating a jQuery plugin? If I were to start coding websites in jQuery, how should I go about it for the best interoperability and design? I want to ensure that my code can run along-side any other jQuery without interference, it’s possible to build plugins off of my code, and I minimize use of the jQuery namespace so that I don’t steal variables that might be used by another script.
Read the jQuery plugin authoring suggestions, also look at the unminified jQuery. Notice the last line:
window.jQuery = window.$ = jQuery;So there are two global variableswindow.jQueryandwindow.$. To delve into this issue a little deeper, read more about the documentation on using jQuery with other libraries andjQuery.noConflict():For writing plugins, make sure to pay special attention to the section called
Maintaining Chainability(since jQuery makes use of chainability so nicely). You have to explicitly returnthisin your plugins to maintain chainability. Additionally, speaking of clashing with other variables, make sure you stop your plugin from clashing with other code by using a closure:There’s a lot of other great information on that plugin authoring page. The above is just the bare bones. There’s info on defaults and options, namespacing, and many other things.
Also, if you’re concerned about your variables clashing, you can also make use of closures for you own “regular” code… not just jQuery plugins. To do this, enclose your script in a self invoking anonymous function:
For example:
jsFiddle example