JQuery notes-Tips for plug-in development and jquery tips

Source: Internet
Author: User

JQuery notes-Tips for plug-in development and jquery tips

On the official jQuery documentation, we recommend that you write plug-ins in the following format. The first time I read the source code, I saw this writing method, which was quite depressing for a long time.

(Function ($ ){
// Code goes here
}) (JQuery );

Below we will record why we write this way. What are the specific advantages of this writing.
 
In fact, the above method is equivalent to the following definition method, but the above method is safer. it is similar to the hidden classes in JAVA. After this definition, a large program will avoid third-party misoperations, thus damaging encapsulation.
 
The above code is equivalent to the following code. I think you can understand it in this way.
Var jQueryFucntion = function ($ ){
// Code goes here
//
}
JQueryFucntion (jQuery );

 
(Function ($ ){
// Code goes here
})
A jQuery function is defined above. The parameter is $
 
 
(Function ($ ){
// Code goes here
}) (JQuery );
This is to call the previously defined function, and then pass the jQuery real parameter in.
 
 
The advantage is that when writing the jQuery plug-in, we can also use the $ alias instead of causing conflicts with prototype.
 
If end-use uses prototype instead of defining it in the above way, it will lead to an irreconcilable conflict.
 


Jquery: How to Develop your own jQuery plugin

1. Declare a specific name in the JQuery namespace
$. Fn. hilight = function (){
// Enter your plug-in Execution code here };
We can call it like this:
$ ('# MyDiv'). hilight ();
2. Receive parameters to control the actions of the plug-in;
To add the foreground and background color for our hilight plug-in, we need to allow an object type option setting in the function. The Code is as follows:
$. Fn. hilight = function (options ){
Var defaults = {
Foreground: 'red ',
Background: 'yellow'}; var opts = $. extend (ults, options );};
Now, our plug-in can be called as follows:
$ ('# MyDiv'). hilight ({
Foreground: 'blue '});
3. Provide configuration item values for the public method Access Plug-in;
The above code can be improved so that the default value of the plug-in can be set outside the plug-in. This is undoubtedly very important, because it allows plug-in users to use the least code to modify plug-in configuration, which is actually the beginning of using function objects.

JQUERY plug-in development object method plug-in

Var Util = {};
Util. test = function (o ){
Defaults = $. extend ({
Some parameters...
}, 0 );
}
$. Extend (Util. test ,{
Getter: function (){
Getter method...
},
Setter: function (){
Setter method...
}
});
Util. test. getter (); // call the getter method externally

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.