Skip to content

Instantly share code, notes, and snippets.

@subtleGradient
Created April 8, 2010 21:51
Show Gist options
  • Select an option

  • Save subtleGradient/360601 to your computer and use it in GitHub Desktop.

Select an option

Save subtleGradient/360601 to your computer and use it in GitHub Desktop.
var Person = Class.subclass(function(def, Protected, Generic, Parent){
// Private
// Available to all methods in this definition
var private_variable;
// Protected
// Available to this Class, its instances, all subclasses and all their instances
// Protected methods are bound to each instance
Protected.property = 123;
Protected.getProperty = function(){
return [private_variable, this.property, Protected.property, Generic.property, Parent.property];
};
// Class level
// Public properties and methods on the new class object itself
Generic.property = 123;
// This is what gets run if you call the class without the `new` operator
// e.g. Dude() vs new Dude()
Generic.run = function(){
throw new Error('This is a Class, you have to use it with the new operator!');
};
Generic.implement(Events);
Generic.addEvent('new', function(instance){
Generic.instances.push(instance);
});
// Instance/Prototype level
// All new instances start out with the same values inherited from their shared prototype
// Anything added to the def (definition) object will be
// unlinked and added the the prototype of the new class
def.arrayProperty = [1,2,3];
def.property = 123;
def.extend(Person);
def.implement(Options, Events);
def.setOptions({
hairColor: 0xFF0000,
hairStyle: 'mullet',
beardLength: -0.125
});
def.initialize = function(options){
Generic.fireEvent('new', this);
this.setOptions(options);
};
def.add = function(value){
this.property += value;
Generic.property += value;
private_variable = value;
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment