After looking through coffeescript's code base, I found a method to build objects from other objects. With a bit of tinkering with the methodology of it, I finally got something that not only works as intented but it QUITE transparent in use.
The snipped below sort of mimics more OOP languages' Class Extension/inheritance methodology but for JAVASCRIPT!!!!!!ONE!!!
To Use:
It's as simple as a function call within the extending object's constructor. this functional call should be the issued BEFORE you start creating methods/functions using things such as 'this.a = "value":
this.extendsUpon(baseObjectConstructor, arg0, arg1, ..., argN)
BaseObjectConstructer: the object inwhich the current constructor should inherit members from
args: the specified args will be passed to the baseObject's Constructor
An example of it's use:
// Base Object Constructor
function Fruit(name) {
this.fruitname = name;
}
Fruit.prototype.yum = function() {
return "I had an " + this.fruitname;
}
// Object constructor that derives from the Base Object
function Favorite() {
// Derive this object from a specified base object:
// @arg0 -> Object Constructor to use as base
// @arg1+ -> arguments passed to the BaseObject's constructor
this.extendsUpon(Fruit, "apple");
// From here proceed as usual
/* To access members from the base object that have been over-written,
* use "this.__base__.MEMBER.apply(this, arguments)" */
}
Favorite.prototype.yum = function() {
return this.__base__.yum.apply(this) + " and it was my favorite";
}
var mmm = new Favorite();
mmm.yum();
// Outputs: "I had an apple and it was my favorite"
Edited to cleanup code
Object.prototype.extendsUpon = (function (_prop, _args) {
return function (base) {
for (var key in base) {
if (_prop.call(base, key)) {
this[key] = base[key];
}
}
function con(child){
this.constructor = child;
}
con.prototype = base.prototype;
this.prototype = new con(this);
this.__base__ = base.prototype;
var args = _args.call(arguments);
args.shift();
base.constructor.apply(this, args);
}
}(Object.prototype.hasOwnProperty, Array.prototype.slice));