Skip to content

"A Lightweight Start" Pattern

Joel Doyle edited this page Apr 12, 2016 · 2 revisions

"A Lightweight Start" Pattern

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

Let’s begin our deeper look at plugin patterns with something basic that follows best practices (including those in the jQuery plugin-authoring guide). This pattern is ideal for developers who are either new to plugin development or who just want to achieve something simple (such as a utility plugin). A Lightweight Start uses the following:

  • Common best practices such as a semi-colon placed before the functions invocation (we'll go through why in the comments below)
  • window, document, undefined passed in as arguments.
  • A basic defaults object.
  • A simple plugin constructor for logic related to the initial creation and the assignment of the element to * work with.
  • Extending the options with defaults.
  • A lightweight wrapper around the constructor, which helps to avoid issues such as multiple instantiations.
  • Adherence to the jQuery core style guidelines for maximized readability.
  1. Code
  2. Usage

Further Reading