Skip to content

"Best Options Pattern" Globally And Per Call Overridable Options

Iurii Kucherov edited this page Aug 5, 2015 · 2 revisions

"Best Options Pattern" Globally And Per Call Overridable Options

From Learning JavaScript Design Patterns, a book by Addy Osmani.

For our next pattern, we’ll look at an optimal approach to configuring options and defaults for a plugin. The way most of us are probably familiar with defining plugin options is to pass through an object literal of defaults to $.extend(), as demonstrated in our basic plugin boilerplate.

If, however, we’re working with a plugin with many customizable options that we would like users to be able to override either globally or on a per-call level, then we can structure things a little more optimally.

Instead, by referring to an options object defined within the plugin namespace explicitly (for example, $fn.pluginName.options) and merging this with any options passed through to the plugin when it is initially invoked, users have the option of either passing options through during plugin initialization or overriding options outside of the plugin (as demonstrated here).

  1. Code
  2. Usage

Further Reading