Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Examples of practical javascript object oriented design patterns

What object oriented design patterns do you use in your application's javascript, and why?

Feel free to post code, even if there is no formal design pattern attached to it.

I have written plenty of javascript, but I have not applied much object orientated patterns to what I am doing, and I am sure i am missing a lot.

like image 999
meow Avatar asked Sep 16 '10 00:09

meow


People also ask

Are design patterns used in JavaScript?

JavaScript modules are the most prevalently used design patterns for keeping particular pieces of code independent of other components. This provides loose coupling to support well-structured code. For those that are familiar with object-oriented languages, modules are JavaScript “classes”.

How many types of design patterns are there in JavaScript?

They are categorized in three groups: Creational, Structural, and Behavioral (see below for a complete list). In this tutorial we provide JavaScript examples for each of the GoF patterns.

Can JavaScript be used for OOP?

JavaScript is a very good language to write object oriented web apps. It can support OOP because supports inheritance through prototyping also properties and methods. You can have polymorphism, encapsulation and many sub-classing paradigms.


1 Answers

The following are three popular JavaScript patterns. These happen to be easily implementable because of closures:

  • The Module Pattern - Example (and made popular) by Eric Miraglia
  • Memoization - Example by Oliver Steele
  • Currying - Example by Dustin Diaz

You may also want to check out:

  • Pro JavaScript Design Patterns by Ross Harmes and Dustin Diaz

The following is a Google I/O talk from 2008 presented by Diaz, where he discusses some topics from his book:

  • Google I/O 2008 - Design Patterns in an Expressive Language
like image 61
Daniel Vassallo Avatar answered Sep 22 '22 17:09

Daniel Vassallo