OOP JS (v2)

by mpetrovich

JavaScript

Class = {
    create : function(name, parent, methods) {
        this[name] = function() {
            methods.init.apply(methods);
        }
    },
    extend : function(parent) {
        function f() {};
        f.prototype = parent.prototype;
        return new f();
    }
};

// =======================================

Class.create("Mammal", null, {
    init : function(type) {
        console.log("Mammal.init(" + type + ")");
    },
    breathe : function() {
        console.log("Mammal.breathe()");
    }
});

var m = Class.Mammal("land");
m.breathe();