Skip to content

Instantly share code, notes, and snippets.

@amatiasq
Created March 21, 2013 18:12
Show Gist options
  • Save amatiasq/5215294 to your computer and use it in GitHub Desktop.
Save amatiasq/5215294 to your computer and use it in GitHub Desktop.
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();
@amatiasq
Copy link
Author

@amatiasq
Copy link
Author

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