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:
http://www.amazon.com/JavaScript-Design-Patterns-Recipes-Problem-Solution/dp/159059908X/ref=sr_1_1?ie=UTF8&qid=1312266239&sr=8-1
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:
http://www.amazon.com/Pro-JavaScript-Techniques-John-Resig/dp/1590597273
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).
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()
{
alert("Load");
},
SaveUser : function()
{
alert("Save");
}
};
var notUser = {
LoadUser : function()
{
alert("Load");
}
};
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);
Inherit.InheritFunc();
Inherit.BaseFunc();
Inherit.AnotherFunc();
Base.BaseFunc();
Base.InheritFunc();
You probably want to look at http://www.mootools.net. It has my favorite implementation of Classes. You also definitely want to check out "Pro Javascript Design Patterns"
With the code defined as I have it in the following fiddle: http://jsfiddle.net/JAAulde/psdym/2/
The best way is to .call() or .apply() A's method inside of B's:
//Inside of B.doSomething
A.prototype.doSomething.call( 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 http://www.amazon.com/JavaScript-Design-Patterns-Recipes-Problem-Solution/dp/159059908X
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: http://www.amazon.com/JavaScript-Design-Patterns-Recipes-Problem-Solution/dp/159059908X/ref=sr_1_1?ie=UTF8&qid=1312266239&sr=8-1
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: http://www.amazon.com/Pro-JavaScript-Techniques-John-Resig/dp/1590597273
Good luck with the reading
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: http://www.amazon.com/JavaScript-Design-Patterns-Recipes-Problem-Solution/dp/159059908X
alt text http://ecx.images-amazon.com/images/I/513vjwS9RpL._BO2,204,203,200_PIsitb-sticker-arrow-click,TopRight,35,-76_AA300_SH20_OU01_.jpg
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:
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):
You probably want to look at http://www.mootools.net. It has my favorite implementation of Classes. You also definitely want to check out "Pro Javascript Design Patterns"
http://www.amazon.com/JavaScript-Design-Patterns-Recipes-Problem-Solution/dp/159059908X
This book goes into good detail about how to emulate OOP in javascript.
With the code defined as I have it in the following fiddle: http://jsfiddle.net/JAAulde/psdym/2/
The best way is to .call() or .apply() A's method inside of B's:
Or, to simply pass all params that came into B.doSomething() in one fell swoop
An amazing book (written by a friend of mine) for learning about various patterns for inheritance in JS is http://www.amazon.com/JavaScript-Design-Patterns-Recipes-Problem-Solution/dp/159059908X