javascript Patterns

Function Patterns

API Patterns

  • Callback patterns – when you pass function A to function B as a parameter, function A is a callback function
  • Configuration objects – keep control of function arguments and makes it easily configurable
  • Returning functions – one function returns another function or create another function on-demand
  • Currying – used to create new functions dynamically by partially applying a set of arguments

Initialization patterns

Performance patterns

  • Memoization – use function properties to avoid repeated computation
  • Self-defining functions – self-overwrite with new bodies to do less work from the second invocation and after

jQuery Patterns

  • requery – avoid requery by using jQuery chaining
  • append – use string concatenate and set innerHTML
  • data – pattern and antipattern of using data
  • context and find – use find over context
  • detach – take element off the DOM while manipulating them
  • event delegation – event delegation pattern and antipattern
  • selector cache – using selector cache to avoid requery
  • window scroll event – avoid attaching handlers to the window scroll event

Selector

Literals and Constructors Patterns

  • Object literal – use the simpler and reliable object literal instead of new Object();
  • Enforcing new – when you forget `new`, `this` inside the constructor will point to the global object
  • Array literal – use array literal notation to avoid potential errors when creating dynamic arrays at runtime
  • Working with JSON – use library from JSON.org, YUI, jQuery instead of eval for parsing
  • Primitive wrappers – try to use the primitive without wrapper
  • Regular expression literal – try to use the shorter literal notation

Design Patterns

Creational

  • Builder – constructs complex objects by separating construction and representation
  • Factory method – creates objects without specifying the exact class to create
  • Singleton – restricts object creation for a class to only one instance

Structural

  • Decorator – dynamically adds/overrides behaviour in an existing method of an object
  • Facade – provides a simplified interface to a large body of code
  • Proxy – provides a placeholder for another object to control access, reduce cost, and reduce complexity

Behavioral

  • Chain of responsibility – delegates commands to a chain of processing objects
  • Command – creates objects which encapsulate actions and parameters
  • Iterator – implements a specialized language
  • Mediator – allows loose coupling between classes by being the only class that has detailed knowledge of their methods
  • Observer – is a publish/subscribe pattern which allows a number of observer objects to see an event
  • Strategy – allows one of a family of algorithms to be selected on-the-fly at runtime

General Patterns

Advertisements

About androiddummies

Android Learner and Developer

Posted on January 23, 2012, in JavaScript, Patterns and tagged . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: