Skip to content

Instantly share code, notes, and snippets.

@BonsaiDen
Created February 1, 2012 23:30
Show Gist options
  • Select an option

  • Save BonsaiDen/1720145 to your computer and use it in GitHub Desktop.

Select an option

Save BonsaiDen/1720145 to your computer and use it in GitHub Desktop.
Crazy stuff... still needs a way to get rid of Members and make it work via closure to keep encapsulation
/**
* Copyright (c) 2012 Ivo Wetzel.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
(global.window || module.exports).Class = function _(ctor) {
var Method = Function;
// Class constructor
// Using new creates a new instance as usual
// omitting it will act like a Super() constructor where the first
// argument is the instance of the subclass
var clas = function Class() {
var args = arguments;
if (this instanceof Class) {
ctor.apply(this, args);
} else {
Method.call.apply(ctor, args);
}
};
// Makes the instanceof check below... work...
// evil trickery to get rid of another global Function for the
// "base" class of all classes
var proto = clas.prototype = new (_.b || (_.b = function(){}))(),
m = clas.Members = {};
// Generic bind function thing
function bind(caller, obj) {
return function() {
return obj.apply(caller, arguments);
};
}
// Extend with members and base classes
var i = 0, a, e, props, val, func;
while((a = arguments[i++])) {
props = a.prototype instanceof _.b ? a.Members : a;
for(e in props) {
if (Method.hasOwnProperty.call(props, e)) {
val = props[e];
func = val instanceof Method;
// Statics methods / props and unbounds methods
if (e[0] === '$') {
clas[e] = func ? bind(clas, val) : val;
} else if (func) {
clas[e] = bind(val, Method.call);
proto[e] = val;
}
m[e] = val;
}
}
}
return clas;
};
var Class = module.exports.Class;
var Foo = Class(function(a, b, c) {
//console.log(this, a, b, c);
this.a = a;
}, {
test: function(text) {
console.log(this, text);
}
});
var e = new Foo(1, 2, 3);
e.test('hello');
var Bar = Class(function(a, b, c) {
Foo(this, a, b, c);
//console.log(this.a);
}, Foo, {
superTest: function(text) {
this.test(text + ' world');
}
});
var f = new Bar(5, 2, 3);
f.superTest('hello');
Foo.test(f, 'hello');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment