0
(function ( $ ) {
    $.fn.greenify = function( options ) {
        // This is the easiest way to have default options.
        var settings = $.extend({
            // These are the defaults.
            color: "#556b2f",
            backgroundColor: "white"
        }, options );
        // Greenify the collection based on the settings variable.
        return this.css({
            color: settings.color,
            backgroundColor: settings.backgroundColor
        });
    };
}( jQuery ));

Taken from Docs the above code shows how to make a simple jquery plugin. What if I only supply one of multiple parameters listed in a plugin that accepts multiple? Which ones are applied?

If I have a plugin that can take multiple parameters, which ones will be applied in something like this:

$.doStuff({color:"black"},{color:"blue"});

What set of options will be "extended" when only one set of parameters if supplied?

E.x. $.doStuff({color:"black"});

1
  • well, typically you won't have multiple parameters, instead you'll have one object with multiple properties. Commented Jan 22, 2014 at 22:13

1 Answer 1

1

If you call a function with more parameters than there are argument variables, and the function doesn't use the special arguments variable to access variable-length arguments, the extra parameters will be ignored.

So in your example:

$.greenify({color: "black"}, {color: "blue"});

the options variable will be set to {color: "black"}, and the {color: "blue"} parameter will be ignored.

Sign up to request clarification or add additional context in comments.

3 Comments

And does that logic follow most cases? So if I have 5 parameters and the plugin accepts 6 the last one is ignored?
Yes. Doesn't that fit the description "more parameters than argument variables"?
I haven't slept in days. please excuse the brain farts. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.