Skip to content

Instantly share code, notes, and snippets.

@subtleGradient
Created February 2, 2010 03:12
Show Gist options
  • Save subtleGradient/292328 to your computer and use it in GitHub Desktop.
Save subtleGradient/292328 to your computer and use it in GitHub Desktop.
Mythical constructor-based Class Implementation Syntax idea
// Create a new class by subclassing Class
var MyClass = Class.subclass(function(Public, Private){
Public.implement([Events, Options]);
Public.method = function(arg){
console.log('grand pappy Method!');
};
Public.methodWithAName = methodWithAName;
function methodWithAName(arg){ /*This method has a name!*/ }
Public.defineMethod('methodWithAName2',function(){
function methodWithAName2(arg){ /*This method has a name that doesn't leak into all the other closures*/ }
return methodWithAName2;
});
});
var MyClass = Class.subclass(function(Instance, Generic){
Generic.method = Instance.method = function(value){
this.hello = value || "world";
};
});
var myClass = new MyClass;
myClass.method("normal");
MyClass.method(myClass, "generic");
// You can even add methods later and still have access to all the same internal variables
MySubClass.define(function(Public, Private, Parent){
Public.method = function(arg){
console.log('child Method!');
Parent.method.call(this); // hard-code method name?
};
});
// or just add a single method
MySubClass.defineMethod('method', function(name, Public, Private, Parent){return function(arg){
console.log('child Method!');
Parent.get(name)(); // Run the bound parent method
this[name](); // recursively call myself
}});
// Create a new subclass by subclassing a Class
var MySubClass = MyClass.subclass(function(Public, Private, Parent){
Public.method = function(arg){
console.log('child Method!');
Parent.get('method')(); // hard-code method name
};
// Parent.get(methodName) could bind the parent method to this instance
// we could then provide the name of the method as an argument
// no hard-coding
Public.defineMethod('method', function(name){return function(arg){
console.log('child Method!');
Parent.get(name)(); // Run the bound parent method
this[name](); // recursively call myself
}});
Public.defineMethod('method', function(parentMethod){return function(arg){
console.log('child Method!');
parentMethod.call(this); // `call` the parent method on this instance
}});
// Should it bind the parent method by default?
// It would have to re-bind to each instance somehow
Public.defineMethod('method', function(boundParentMethod){return function(arg){
console.log('child Method!');
boundParentMethod(); // Run the bound parent method
}});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment