Nested Inheritance with composition

Use composition and inheritance to augment the nesting upward and downward.

by gschutz

HTML

<script src="https://cdn.rawgit.com/lodash/lodash/3.10.1/lodash.js"></script>

JavaScript

!function(exports) {
    var extensions = [];
    
    function $applyExtension(source, ext) {
        source[ext.name] = ext.constructor.call(source);
    }
    
    function $extend(source) {
        return function(name, constructor) {
            var ext = {parent: source.name || this.name, name: name, constructor: constructor};
            
            ext.$extend = $extend.call(ext, ext.constructor);
            
            extensions.push(ext);
            this[name] = ext;
            
            return this;
        };
    }
    
    function Neomap() {
        this.$options = {};
    }
    
    function neomap() {
        var instance = new (Function.prototype.bind.apply(Neomap, [Neomap].concat(_.toArray(arguments))));
        
        // initialize each extension
		_.each(_.filter(extensions, {parent: 'Neomap'}), function(ext) {
            $applyExtension(instance, ext);
        });
        
        return instance;
    }
    
    neomap.$extend = $extend.call(neomap, Neomap);
    
    
    exports.Neomap = neomap;
}(window);


Neomap.$extend('Teste', function() {
    
    console.log(this);
    
    return function Test(name){
        this.name = name || "";
        this.ola = "er3";
    };
});

console.log(Neomap.Teste);

Neomap.Teste.$extend('Jasmine', function() {
    return function Jasmine(){};
});

console.log(Neomap.Teste.Jasmine);

var neomap = Neomap();

console.log(neomap.Teste);