Ian Hoar
Ian Hoar

Reputation: 1184

jQuery plugin defaults

Is there a way to change the defaults within a plugin after they have been set. For example.

var defaults = {
 imageWidth : w,
 imageHeight : h
};

Later in the code I want to change the imageWidth value again.

Upvotes: 0

Views: 399

Answers (1)

mu is too short
mu is too short

Reputation: 434635

Maybe, depends on the plugin. If it looks like this:

(function() {
    var defaults = { /* ... */ };
    // Rest of the plugin goes here.
})();

Then you're out of luck. However, quite a few plugins publish their defaults as:

$.fn.plugin.defaults

Where plugin is the plugin name. If the defaults are publicly accessible like that (or if you want to hack the plugin to make them public), then you can change the defaults whenever you want. Of course, the new defaults won't apply to existing instances.


If this your own plugin then I think your architecture is a bit off. The "full screen" versus "specified width and height" configuration should be an option for the plugin and leave the default as something sensible (where "sensible" is, as always, context dependent). Then you'd just say this:

$(x).plugin({ fullscreen: true }); // Full screen
// or
$(y).plugin({ w: 11, h: 23 });     // Specific dimensions

You could also use the $.fn.plugin.defaults trick to allow the sensible defaults to be changed as needed.

Upvotes: 3

Related Questions