Pro JavaScript Design Patterns: The Essentials of Object-Oriented JavaScript Programming

Category: Programming
Author: Dustin Diaz, Ross Harmes
This Month Stack Overflow 4


by anonymous   2019-07-21

It's just a simple example, it isn't meant to be object-oriented.

Most design patterns can be implemented with JavaScript, this book may help:

I've read it, it's ok. In my opinion John Resig's book 'Pro JavaScript techniques' is the best in order to learn JavaScript the good way:

Good luck with the reading

by anonymous   2019-07-21

APress has a book called "Pro Javascript Design Patterns" that is probably just what you're looking for. While the author (Diaz Harmes) is certainly not "the gang of four", I think he does a pretty good job; I know I learned quite a bit from it (and it got 4.5 stars on Amazon).

Amazon link:

alt text,204,203,200_PIsitb-sticker-arrow-click,TopRight,35,-76_AA300_SH20_OU01_.jpg

by Nelson LaQuet   2019-07-21

You are looking at two different things.

First you have interfaces. The most accepted way of implementing this is though Duck Typing ("if it looks like a duck and quacks like a duck then it is a duck"). This means that if an object implements a set of methods of the interface then it is that interface. You implement this by having an array of method names that define an interface. Then to check if an object implements that interfece you see if it implements those methods. Here is a code example I whipped up:

function Implements(obj, inter)
    var len = inter.length, i = 0;
    for (; i < len; ++i)
        if (!obj[inter[i]])
            return false;
    return true;

var IUser = ["LoadUser", "SaveUser"];

var user = {
        LoadUser : function()

        SaveUser : function()

var notUser = {
        LoadUser : function()

alert(Implements(user, IUser));
alert(Implements(notUser, IUser));

Now you have inheritance. JS has no inheritance built in; so you have to implement it manually. This is just a matter of "copying" the properties of one object to another. Here is another code sample (not perfect but it demonstrates the point):

function InheritObject(base, obj)
    for (name in base)
        if (!obj[name])
            obj[name] = base[name];

var Base = {
        BaseFunc : function() { alert("BaseFunc from base"); },
        InheritFunc : function() { alert("InheritFunc from base"); }

var Inherit = {
        InheritFunc : function() { alert("InheritFunc from inherit"); },
        AnotherFunc : function() { alert("AnotherFunc from inherit"); }

InheritObject(Base, Inherit);



You probably want to look at It has my favorite implementation of Classes. You also definitely want to check out "Pro Javascript Design Patterns"

This book goes into good detail about how to emulate OOP in javascript.

by anonymous   2019-07-21

With the code defined as I have it in the following fiddle:

The best way is to .call() or .apply() A's method inside of B's:

//Inside of B.doSomething this, arg1, arg2 );

Or, to simply pass all params that came into B.doSomething() in one fell swoop

//Inside of B.doSomething
A.prototype.doSomething.apply( this, arguments );

An amazing book (written by a friend of mine) for learning about various patterns for inheritance in JS is