Use the jquery wrapper utility function

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.