First, Introduction
Project development, the front end will have a secondary tool class JS files, such as the operation of cookies, team members themselves encapsulated methods. Most of the time, the developers themselves write a global function, regardless of the post-maintenance staff will write the same code, and then create conflicts between the code. A section of code that was previously maintained is as follows:
var g=function(ID) { return document.getElementById (ID);}; var $$=function(ID) { return document.getElementById (ID);}; G ("Testdiv"). style.color= "Red"; $$ ("Testdiv"). Innerhtml= "This is a test.";
Because the code is old, I don't know if it was written before jquery came out, or after that. Developers in order to save the back to write document.getElementById () This method, the page actually used two abbreviations to replace. $$ should be added by the developers behind it, and maybe he feels that using G instead of document.getElementById () is not obvious. This also shows that in the development process, many developers in order to go to large-scale modification of other people's code, we like to add content in other people's code, do not want to delete content. The project can be run, but a lot of redundant code is produced, which is very detrimental to later maintenance and refactoring. The above is just an example of a practical project.
Second, examples
After the introduction of jquery, most developers prefer to replace them with $. So far, I've used a third-party class library That's basically not defined by $. So there's basically no jquery object represented by $. But for code maintainability, and in our writing code, the $ is jquery object, we need to write the function safely. Because sometimes the names of the projects are different, we also use namespaces to differentiate them.
(function($) { $.say=function(what) { alert ("I say" + What);} }) (jQuery);
Here we pass the jquery object into the function to ensure that the $ that we use inside the function is the jquery object.
A city Complex example tags: Jquery,jquery plugin, javascript
Use the jquery wrapper utility function