A
aum
Hi,
I'm a Python programmer, just starting to get into javascript. On reading
some of the js guides, and not liking any of the OO usage patterns I saw,
I've cooked up something which python folks might find to taste.
Here's the code - first the 'engine', then some code demonstrating the
usage patterns.
For me (and maybe for some of you), it promotes readability and some
pattern similarity for classical OO languages.
Cheers
aum
*** start code ***
// this is the 'engine' of it all - usage patterns follow
Object.prototype.subclass = function(props) {
// extract 'init' function from property list, or
// use a null default function
if (props.hasOwnProperty('init')) {
constructor = props['init']
}
else {
constructor = function() {}
}
// set up inheritance
constructor.prototype = new this();
// populate prototype object with all properties except init
for (key in props) {
if (key != 'init' && key != 'subclass') {
//alert("Inheriting property '" + key + "'");
constructor.prototype[key] = props[key];
}
}
// done
return constructor;
}
// USAGE PATTERNS
// Create 'employee' class
employee = Object.subclass({
init: function(name) {
this.name = name;
},
getname: function() {
return this.name;
},
toString: function() {
return "[employee: "+this.name+"]";
}
})
// Create engineer class
engineer = employee.subclass({
init: function(name, project) {
// invoke parent initialiser
employee.apply(this, [name]);
// do local initialisations
this.project = project;
},
getproject: function() {
return this.project;
},
toString: function() {
return "[engineer: "+this.name+"(" + this.project + ")]";
}
})
// Create 'engineer's assistant' class
assistant = engineer.subclass({
init: function(name, boss) {
// invoke parent initialiser
engineer.apply(this, [name, boss.project]);
this.boss = boss
},
getboss: function() {
return this.boss;
},
toString: function() {
return "[assistant: "+this.name + " (reporting to "+this.boss+")]";
},
})
alert("Constructing employee 'fred'...");
fred = new employee("Fred Dagg")
alert("Constructing engineer 'mary'...");
mary = new engineer("Mary Smith", "Networks")
alert("Constructing assistant 'jim'...");
jim = new assistant("Jim Jones", mary)
alert("fred.name='" + fred.name + "'");
alert("fred.getname() = '" + fred.getname() + "'");
alert("mary.project='" + mary.project + "'");
alert("mary.getproject() = '" + mary.getproject() + "'");
alert("mary.name='" + mary.name + "'");
alert("mary.getname() = '" + mary.getname() + "'");
alert("jim.name='" + jim.name + "'");
alert("jim.boss='" + jim.boss.name + "'");
I'm a Python programmer, just starting to get into javascript. On reading
some of the js guides, and not liking any of the OO usage patterns I saw,
I've cooked up something which python folks might find to taste.
Here's the code - first the 'engine', then some code demonstrating the
usage patterns.
For me (and maybe for some of you), it promotes readability and some
pattern similarity for classical OO languages.
Cheers
aum
*** start code ***
// this is the 'engine' of it all - usage patterns follow
Object.prototype.subclass = function(props) {
// extract 'init' function from property list, or
// use a null default function
if (props.hasOwnProperty('init')) {
constructor = props['init']
}
else {
constructor = function() {}
}
// set up inheritance
constructor.prototype = new this();
// populate prototype object with all properties except init
for (key in props) {
if (key != 'init' && key != 'subclass') {
//alert("Inheriting property '" + key + "'");
constructor.prototype[key] = props[key];
}
}
// done
return constructor;
}
// USAGE PATTERNS
// Create 'employee' class
employee = Object.subclass({
init: function(name) {
this.name = name;
},
getname: function() {
return this.name;
},
toString: function() {
return "[employee: "+this.name+"]";
}
})
// Create engineer class
engineer = employee.subclass({
init: function(name, project) {
// invoke parent initialiser
employee.apply(this, [name]);
// do local initialisations
this.project = project;
},
getproject: function() {
return this.project;
},
toString: function() {
return "[engineer: "+this.name+"(" + this.project + ")]";
}
})
// Create 'engineer's assistant' class
assistant = engineer.subclass({
init: function(name, boss) {
// invoke parent initialiser
engineer.apply(this, [name, boss.project]);
this.boss = boss
},
getboss: function() {
return this.boss;
},
toString: function() {
return "[assistant: "+this.name + " (reporting to "+this.boss+")]";
},
})
alert("Constructing employee 'fred'...");
fred = new employee("Fred Dagg")
alert("Constructing engineer 'mary'...");
mary = new engineer("Mary Smith", "Networks")
alert("Constructing assistant 'jim'...");
jim = new assistant("Jim Jones", mary)
alert("fred.name='" + fred.name + "'");
alert("fred.getname() = '" + fred.getname() + "'");
alert("mary.project='" + mary.project + "'");
alert("mary.getproject() = '" + mary.getproject() + "'");
alert("mary.name='" + mary.name + "'");
alert("mary.getname() = '" + mary.getname() + "'");
alert("jim.name='" + jim.name + "'");
alert("jim.boss='" + jim.boss.name + "'");