473,386 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

prototypal inheritance using beget

I am planning on writing a fairly large system in javascript and would
like some advice on using the Douglas Crockford 'beget' function to
implement prototypal inheritance. Searching the internet there don't
seem to be any practical examples of javascript prototypal inheritance
(or maybe I can't find them).

As a start I have created the following example code and would like to
know if I am going in the right direction?
Is there a better way to do this?

Is it correct that any manager object (in this case "bob") gets used
as the base of the new director object?
Object.prototype.beget = function () {
function F() {}
F.prototype = this;
return new F();
};

// Set up Base object
var employee = {
setName: function(name) {
this.name = name;
}
};

// Manager maker
function managerFactory(emp) {
var aManager = emp.beget();
aManager.smokeCigar = function() {
alert(this.name + ' is smoking');
};
aManager.whipEmployees = function() {
alert(this.name + ' is whipping');
};
return aManager;
}

// Director maker
function directorFactory(man) {
var aDirector = man.beget();
aDirector.tiffin = function() {
alert(this.name + ' is enjoying tiffin with secretary');
};
return aDirector;
}

var bob = managerFactory(employee);
bob.setName('bob');
bob.smokeCigar();

var harold = directorFactory(bob);
harold.setName('harold');
harold.whipEmployees();
harold.tiffin();
Thanks,
Lincoln.

Jun 20 '07 #1
3 2093
On Jun 20, 5:34 am, linc <lincolncoo...@gmail.comwrote:
I am planning on writing a fairly large system in javascript and would
like some advice on using the Douglas Crockford 'beget' function to
implement prototypal inheritance. Searching the internet there don't
seem to be any practical examples of javascript prototypal inheritance
(or maybe I can't find them).

As a start I have created the following example code and would like to
know if I am going in the right direction?
Is there a better way to do this?

Is it correct that any manager object (in this case "bob") gets used
as the base of the new director object?

Object.prototype.beget = function () {
function F() {}
F.prototype = this;
return new F();

};

// Set up Base object
var employee = {
setName: function(name) {
this.name = name;
}

};

// Manager maker
function managerFactory(emp) {
var aManager = emp.beget();
aManager.smokeCigar = function() {
alert(this.name + ' is smoking');
};
aManager.whipEmployees = function() {
alert(this.name + ' is whipping');
};
return aManager;

}

// Director maker
function directorFactory(man) {
var aDirector = man.beget();
aDirector.tiffin = function() {
alert(this.name + ' is enjoying tiffin with secretary');
};
return aDirector;

}

var bob = managerFactory(employee);
bob.setName('bob');
bob.smokeCigar();

var harold = directorFactory(bob);
harold.setName('harold');
harold.whipEmployees();
harold.tiffin();
I'm not a prototype-based OO guru, but what you say is right. Any
object can begat another one. You can use any manager object to beget
any director object.

Although to me it makes sense to do this

var manager = managerFactory(employee);
var bob = manager.beget();
bob.smokeCigar();

// all other managers will be begat from the manager object

var director = directorFactory(manager);
var harold = director.beget();
harold.tiffin();

// all other directors will be begat from the director obj

maybe depending on your logic this can happen too

var bob = employee.beget();

// later...

bob = managerFactory(bob);

// much later after much brown-nosing

bob = directorFactory(bob);
bob.tiffin();
That's what's cool about prototype-based OO, it's so flexible. But
once again, I'm not experience in prototype-based OO myself.

If you want to know about best practices of prototype-based OO, you're
right, there's not much out there on the internet about it. Maybe
hanging around the Self or Io group can probably help you in prototype-
based OO best practices.

Jun 20 '07 #2
On Wed, 20 Jun 2007 at 09:34:45, in comp.lang.javascript, linc wrote:

<snip>
>Searching the internet there don't
seem to be any practical examples of javascript prototypal inheritance
(or maybe I can't find them).
<snip>

There's a practical example in my web site,

<URL:http://www.jgharris.demon.co.uk/jsfeats/JSfeats.html#seca1p1>

but it doesn't use beget.

John
--
John Harris
Jun 20 '07 #3
Although to me it makes sense to do this

var manager = managerFactory(employee);
var bob = manager.beget();
bob.smokeCigar();
Yes - this makes things much clearer - thanks.
Lincoln

Jun 21 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Graham Banks | last post by:
Does using multiple inheritance introduce any more performance overhead than single inheritance?
4
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that...
14
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and obvious in a fully OOP language, but is not at...
12
by: Steve Jorgensen | last post by:
The classing Visual Basic and VBA support for polymorphism, let's face it, is a bit on the weak side, and built-in support for inheritance is non-existent. This little essay is about some patterns...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
5
by: Invalidlastname | last post by:
Hi, I just read the pattern "Design and Implementation Guidelines for Web Clients" from MSDN. Here is my question. In chapter 3,...
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
7
by: Jessica | last post by:
Hi, I have a question referring prototypal inheritance in javascript. My example: function A() {}; A.prototype = { count: 10, doit : function () {alert ("doit");} };
3
by: Leo Seccia | last post by:
Hello everyone, I have a c# project with a sql server database. I have a number of lookup tables in my database which I successfully managed to import into my LINQ dataclasses. eg. Table:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.