prototypal inheritance using beget

L

linc

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.
 
J

Jang

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.
 
L

linc

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top