Created
March 21, 2013 18:12
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
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
Based on the idea found here: http://www.2ality.com/2011/12/fake-operator-overloading.html