Help me to make reusable code

Extract.

1 Like

How to use it?

Pull out the options being passed to owlCarousel to separate named variables, then find ways to group together common aspects of those options.

Use functions to encapsulate common behaviour, so that it’s easier to read the code and understand the intention of what is wanting to be achieved.

How do I group them?
Help me with sample code.

What kind of reusability did you have in mind?

In the above code I’m repeating everything except responsive object. So I want to avoid redundancy.

Use a function instead. That function can have a base object and based on the name that you pass to the function, such as “hp”, “rec”, or “fav”, the function can modify the base object and return it with the appropriate modifications.

For example:

function catOptions(type) {
    var opts = {
        legs: 4,
        tailLength: 0.30
    };
    if (type === "ginger") {
        opts.gender = "Male";
    }
    if (type === "lynx") {
        opts.tailLength = 0;
    }
    return opts;
}
var cat1Opts = catOptions("ginger");
var cat2Opts = catOptions("lynx");

Thanks. :slight_smile: I used jQuery plugins way.

$.fn.sdSlider = function(options){

}

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.