Skip to content

Instantly share code, notes, and snippets.

@ibolmo
Forked from subtleGradient/Class.js
Created February 2, 2010 04:08
Show Gist options
  • Save ibolmo/292349 to your computer and use it in GitHub Desktop.
Save ibolmo/292349 to your computer and use it in GitHub Desktop.
// Mythical constructor-based Class Implementation Syntax
// Create a new class by subclassing Class
var MyClass = Class(function(){
this.initialize = function(){
console.log('MyClass::initialize')
};
this.method = function(argument){
console.log('grand pappy Method!');
};
this._.method = function(){
console.log('only available to parent');
};
});
// Create a new subclass by subclassing a Class
var MySubClass = MyClass.subclass(function(parent){
this.initialize = function(){
parent.call(this);
};
this.method = function(arg1, arg2, arg3){
console.log('child Method');
parent.method.apply(this, arguments);
};
});
MySubClass.implement(function(parent){
this.method2 = function(){
parent.method2.call(this);
if (false) this.method2();
};
});
// Since creator functions are just functions that get passed certain arguments,
// we can define some default builder functions
var defaultPropertyCreator = function(Property, Public, Private){
Property.get = function(){
return Private[Property.key];
};
Property.set = function(value){
Private[Property.key] = value;
};
return Property;
};
// You can even add crazy mutated methods!
MySubClass.defineProperty('hairColor', defaultPropertyCreator);
MySubClass.defineProperty('eyeColor', defaultPropertyCreator);
MySubClass.defineProperties({
height:defaultPropertyCreator,
width: function(a,c,b){
defaultPropertyCreator(a,b,c);
a.default = 0.5;
}
});
// Instantiation
var myClass = new MyClass;
var mySubClass = new MySubClass;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment