Skip to content

Instantly share code, notes, and snippets.

@madx
Created June 11, 2013 07:46
Show Gist options
  • Save madx/5755099 to your computer and use it in GitHub Desktop.
Save madx/5755099 to your computer and use it in GitHub Desktop.
var defineClass = function(classDefinition) {
var constructor = function() {
if ("initialize" in this) this.initialize(arguments)
}
if (classDefinition) classDefinition.call(constructor.prototype, constructor)
return constructor
}
var inherit = function(childClass, parentClass) {
var p, proto = parentClass.prototype
for (p in proto)
childClass.prototype[p] = proto[p]
if (proto.initialize)
proto.initialize.call(this)
}
var Class = defineClass(function() {
// Public interface, "this" is the prototype
this.initialize = function() {
console.log("init")
}
this.foo = function() {
bar()
}
// Private methods
var bar = function() { console.log("baz") }
})
var SubClass = defineClass(function(constructor) {
this.initialize = function() {
inherit.call(this, constructor, Class)
inherit.call(this, constructor, Mixin) // Mixins use the same mechanism
}
})
var Mixin = defineClass(function() {
this.toString = function() {
return "MIXIN ENABLED{" + {}.toString.call(this) + "}"
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment