Created
February 2, 2010 03:12
-
-
Save subtleGradient/292328 to your computer and use it in GitHub Desktop.
Mythical constructor-based Class Implementation Syntax idea
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
}}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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