Javascript Objects built atop other Object Constructors

By SReject on Nov 12, 2012

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();

// 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 (, 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 =;
        base.constructor.apply(this, args);
}(Object.prototype.hasOwnProperty, Array.prototype.slice));


Sign in to comment.
sean   -  Nov 15, 2012

@Hawkee Agreed. Many languages are built on top of others :)

Hawkee   -  Nov 15, 2012

Right, but it is a language that is "compiled". I think its better to call it a language than a framework.

SReject   -  Nov 15, 2012

CoffeeScript isn't a new language. New syntax, but when 'compiled' it's source is transcoded into javascript

Hawkee   -  Nov 15, 2012

Would certainly get more attention posting CoffeeScript. People like seeing code for new languages.

SReject   -  Nov 15, 2012

Won't get any from me. Im an old fashion JS programmer

Hawkee   -  Nov 15, 2012

Now let's see some CoffeeScript. I added it to the site as a supported language.

SReject   -  Nov 13, 2012

Fixed a few typos in the code.

Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.