10 suggestions to help you create better jQuery plug-ins

Source: Internet
Author: User

10 suggestions to help you create better jQuery plug-ins

This article summarizes 10 suggestions to help you create a better jQuery plug-in. Share it with you for your reference. The details are as follows:

After many jQuery plug-ins have been developed, I have developed a set of relatively standard structures and modes for developing jQuery plug-ins. In this way, I can copy and paste most of the code structure, as long as I focus on the most important logic code. Using the same design pattern and architecture makes fixing bugs or secondary development easier. A verified architecture ensures that my plug-ins do not have major problems, whether simple or complex. I will share 10 of my experiences here.

1. Put all your code in the closure

This is the most widely used one. But sometimes Methods outside the closure cannot be called.

However, your plug-in code only serves your own plug-in, so this problem does not exist. You can put all the code in the closure.

Methods may be placed inside the Prototype method ,.

?

1

2

3

(Function ($ ){

// Code here

}) (JQuery );

2. provide default options for plug-ins

Your plug-in should have some options that developers can set, so it is necessary to provide the default options for restoring. You can use the extend function of jQuery to set these options:

?

1

2

3

4

5

Var defaultSettings = {mode: 'pencil ',

LineWidthMin: '0 ',

LineWidthMax: '10 ',

LineWidth: '2 '};

Settings = $. extend ({}, defaultSettings, settings || {});

3. Return an element

A good feature of JavaScript/jQuery is that it can perform method cascade. Therefore, we should not destroy this feature and always return an element in the method. I followed this line in each of my jQuery plug-ins.

?

1

2

3

4

5

6

$. Fn. wPaint = function (settings ){

Return this. each (function (){

Var elem = $ (this );

// Run some code here

}

}

4. Put one-time code out of the Main Loop

This is important, but is often ignored. To put it simply, if you have a piece of code with a bunch of default values, you only need to instantiate it once, instead of instantiating it every time you call your plug-in function, you should put this code out of the plug-in method. This allows your plug-in to run more efficiently and save memory.

?

1

2

3

4

5

6

7

8

9

10

11

12

Var defaultSettings = {

Mode: 'pencil ',

LineWidthMin: '0 ',

LineWidthMax: '10 ',

LineWidth: '2 '};

Settings = $. extend ({}, defaultSettings, settings || {});

$. Fn. wPaint = function (settings ){

Return this. each (function (){

Var elem = $ (this );

// Run some code here

}

}

You can notice that the "defaultSettings" in the above Code is completely out of the plug-in method, because the code is in the closure, we don't have to worry about these variables being overwritten.

5. Why set Class Prototyping?

As the blood and flesh of your code, methods and functions should be placed in the prototype function. There are two reasons:

◆ It can save a lot of memory because you do not need to create these methods again.

◆ Referencing a ready-made method is much faster than creating a new method.

Simply put, prototype extends an object and provides methods for it, instead of instantiating these methods in each object. This also makes your code more organized and efficient. Once you get used to this development method, you will find that it saves you a lot of time in your future projects.

6. How to Set Class Prototyping

The prototype method has two parts. First, we need to create our original class definition. In most cases, this means creating an object. This definition contains different parts of each object instance. In my Paint jQuery Plugin plug-in, I wrote this:

?

1

2

3

4

5

6

7

Function Canvas (settings ){

This. settings = settings;

This. draw = false;

This. canvas = null;

This. ctx = null;

Return this;

}

To add a global method, follow these steps:

?

1

2

3

4

5

Canvas. prototype = {

Generate: function (){

// Generate code

}

}

The key here is to make the prototype method generic, but the data is owned by each instance and can be referenced with "this.

7. Use the "this" object

By using "$ this", we can pass correct references to other closures. We may also need to input $ this reference to other methods. Note that the $ this name can be changed, and any variable name can be changed.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

Canvas. prototype = {

Generate: function (){

// Some code

Var $ this = this;

Var buton = //... some code

Button. click (function (){

// Using this will not be found since it has it's own this

// Use $ this instead.

$ This. someFunc ($ this );

});

},

SomeFunc: function ($ this ){

// Won't know what "this" is.

// Use $ this instead passed from the click event

}

}

8. Save settings in each object

I keep saving my settings in every object and then operating on its own settings. In this way, you do not need to pass many parameters in different methods. Put these variables in the object so that you can call them elsewhere.

?

1

2

3

4

Function Canvas (settings ){

This. settings = settings;

Return this;

}

9. Separate your Prototype method Logic

This may be a basic principle. When you are hesitant to provide a method, you can ask yourself, "If someone else wants to rewrite this method, can your code meet your needs ?" Or "how difficult is it for someone to write this method ?". Of course, this is a matter of flexibility. The method of my Color Picker jQuery Plugin is listed here. For details, refer:

?

1

2

3

4

5

6

7

8

Generate ()

AppendColors ()

ColorSelect ()

ColorHoverOn ()

ColorHoverOff ()

AppendToElement ()

ShowPalette ()

HidePalette ()

10. Provides the Setter/Getter Option

This is not mandatory, but I found that all my plug-ins use this one. Because it only requires a little bit of code, it can provide a function that someone else may need.

Basically, we only need to allow developers to set or retrieve the existing values of the element:

?

1

2

Var lineWidth = $ ("# container"). wPaint ("lineWidth ");

$ ("# Container"). wPaint ("lineWidth", "5 ");

Conclusion: The above 10 articles cover the core of jQuery plug-in development and can be used as development templates. A basic set of code can greatly shorten your development time and make you more confident when designing the plug-in architecture.

I hope this article will help you with jquery programming.

Related Article

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.