Wednesday 14 December 2011

JavaScript Patterns

Being a client side language usually heavily tied to the UI, JavaScript code sometimes suffers from a lack of core programming concepts that can make the code hard to reuse and maintain. Here are a few patterns that can help:

Prototype

Benefits include:

  • Variables taken out of global namespace
  • Methods loaded once into memory and reused by each instance
  • Methods can be overridden by developers using your code

image

Module

Benefits include:

  • Variables taken out of global namespace
  • Public and private members

Cons:

  • Methods get loaded into memory for each instance
  • Not easy to override methods
  • Debugging can be trickier as private members can be hidden

image

And the winner is… Revealing Prototype

Benefits include:

  • Variables taken out of global namespace
  • Public and private members
  • Easy to override methods

image

No comments:

Post a Comment