Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A implementation of the fake-operator-overload inheritance. (http://www.2ality.com/2011/12/fake-operator-overloading.html) Just for fun
(function(root) {
var current = {
parent: null,
config: null,
};
function clazz(name) {
function Class(config) {
if (this instanceof Class)
return this.init.apply(this, arguments);
current.parent = Class;
current.config = config || {};
}
Class.classname = name;
function result(proto) {
Class.prototype = proto || Class.prototype;
window[name] = Class;
}
result.valueOf = function valueOf() {
var config = current.config;
var proto = Object.create(current.parent.prototype);
Object.keys(config).forEach(function(prop) {
proto[prop] = config[prop];
});
proto.constructor = Class;
Class.prototype = proto;
window[name] = Class;
};
return result;
}
root.clazz = clazz;
})(this);
clazz('Person')({
init: function() {
this.greet('normal person');
},
greet: function(what) {
console.log("Hi, I'm a " + what);
}
});
clazz('Ninja') << Person ({
init: function() {
this.greet('super ninja!');
}
});
var test = new Ninja();
Owner

amatiasq commented Mar 21, 2013

Wonder what would happen if this idea is mixed with the jsBase .base() method? :P

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment