Skip to content

Instantly share code, notes, and snippets.

@subtleGradient
Created February 11, 2010 02:28
Show Gist options
  • Select an option

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

Select an option

Save subtleGradient/301136 to your computer and use it in GitHub Desktop.
mySubClass = MyClass.subclass(function(){ … })
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;
};
});
var Person = Class.subclass(function(def, Protected, Generic, Parent, Global){
Generic.implement(Events);
Generic.addEvent('new', function(instance){
Generic.instances.push(instance);
});
Generic.run = function(){
throw new Error('This is a Class, you have to use it with the `new` operator!');
};
Generic.initialize = function(options){ // aka `def.initialize`
Generic.fireEvent('new', this);
this.setOptions(options);
};
Protected.getProperty = function(){
return [private_var,
this .property,
Protected .property,
Generic .property,
Parent .property,
Global .property];
};
var private_var = 0;
def .property = 1;
Protected .property = 2;
Generic .property = 3;
Parent .property = 4;
Global .property = 5;
return {
Implements: [Options, Events],
options: {
hairColor: 0xFF0000,
hairStyle: 'mullet',
beardLength: -0.125
},
add: function(value){
private_var += value;
this .property += value;
Protected .property += value;
Generic .property += value;
Parent .property += value;
Global .property += value;
}
};
});
var Person = new Class({
Implements: [Events, Options],
options: {
name: 'Edgar',
age: 42
},
initialize: function(options){
this.setOptions(options);
},
say: function(message){
alert(this.options.name + ' says: "' + message + '"');
return this;
}
});
var Dude = new Class({
Extends: Person,
say: function(message){
return this.parent(message + '… dude!');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment