Skip to content
Browse files

rebase

rework class against base.js & jsresig + add more tests + fix mixin logic

update build scripts
  • Loading branch information...
1 parent 4748c6f commit 0f9f6fdf140520a6dba3450815d3914f898882e1 @fat fat committed
Showing with 129 additions and 155 deletions.
  1. +43 −63 klass.js
  2. +1 −1 klass.min.js
  3. +1 −1 make/build.js
  4. +43 −63 src/klass.js
  5. +1 −1 test/test.html
  6. +40 −26 test/tests.js
View
106 klass.js
@@ -3,74 +3,54 @@
* https://github.com/polvero/klass
* MIT License
*/
-!function (context) {
-
- function noop() {}
-
- function methods(o) {
- for (var k in o) {
- o.hasOwnProperty(k) && wrapper.call(this, o, k);
- }
- return this;
- }
-
- function statics(o) {
- for (var k in o) {
- o.hasOwnProperty(k) && (this[k] = o[k]);
- }
- return this;
- }
-
- function wrapper (o, k) {
- var sup = this.prototype.constructor.sup;
- this.prototype[k] = function () {
- this._name = k;
- this.sup = sup;
- return o[k].apply(this, arguments);
- };
- }
-
- function klass(fn) {
- var o = typeof fn != 'function' && fn;
- fn = (typeof fn == 'function') ? fn : noop;
-
- fn.methods = methods;
- fn.statics = statics;
- fn.extend = extend;
- fn.prototype.implement = implement;
- return o ? fn.methods(o) : fn;
- }
-
- function extend(sub) {
- var sup = this;
+!function(context){
+ var fnTest = /xyz/.test(function(){xyz;}) ? /\bsupr\b/ : /.*/, noop = function(){};
+
+ this.klass = function(o){
+ var methods, _constructor = typeof o == 'function' ? (methods = {}, o) : (methods = o, noop);
+ return klass.extend.call(_constructor, o);
+ };
+
+ klass.extend = function(o) {
+
+ var supr = this,
+ _methods,
+ _constructor = typeof o == 'function' ? (_methods = {}, o) : (_methods = o, this),
+ fn = function fn() {
+ supr.apply(this, arguments);
+ _constructor.apply(this, arguments);
+ };
+
+ var prototype = new noop();
+
+ fn.methods = function (prop) {
+ for (var name in prop) {
+ prototype[name] = typeof prop[name] == "function" &&
+ typeof supr.prototype[name] == "function" && fnTest.test(prop[name]) ?
+ (function(name, fn){
+ return function() {
+ this.supr = supr.prototype[name];
+ return fn.apply(this, arguments);
+ };
+ })(name, prop[name]) :
+ prop[name];
+ }
- function fn() {
- sup.apply(this, arguments);
- typeof sub == 'function' && sub.apply(this, arguments);
+ fn.prototype = prototype;
+ return this;
}
- var F = function (){};
- F.prototype = sup.prototype;
- fn.prototype = new F();
- klass(fn);
-
-
- fn.prototype.constructor = fn;
- fn.prototype.constructor.sup = sup;
- fn.prototype.supr = function () {
- if (this.sup.prototype[this._name]) {
- return this.sup.prototype[this._name].apply(this, arguments);
+ fn.methods.call(fn, _methods).constructor = this;
+ fn.extend = arguments.callee;
+ fn.prototype.implement = fn.statics = function (o) {
+ for (var k in o) {
+ o.hasOwnProperty(k) && (this[k] = o[k]);
}
- };
-
- return (typeof sub != 'function' && sub && fn.methods(sub)) || fn;
- }
-
- function implement(o) {
- this.constructor.methods(o);
- return this;
- }
+ return this;
+ }
+ return fn;
+ };
if (typeof module !== 'undefined' && module.exports) {
module.exports = klass;
View
2 klass.min.js
@@ -3,4 +3,4 @@
* https://github.com/polvero/klass
* MIT License
*/
-!function(a){function h(a){this.constructor.methods(a);return this}function g(a){function c(){b.apply(this,arguments),typeof a=="function"&&a.apply(this,arguments)}var b=this,d=function(){};d.prototype=b.prototype,c.prototype=new d,f(c),c.prototype.constructor=c,c.prototype.constructor.sup=b,c.prototype.supr=function(){if(this.sup.prototype[this._name])return this.sup.prototype[this._name].apply(this,arguments)};return typeof a!="function"&&a&&c.methods(a)||c}function f(a){var e=typeof a!="function"&&a;a=typeof a=="function"?a:b,a.methods=c,a.statics=d,a.extend=g,a.prototype.implement=h;return e?a.methods(e):a}function e(a,b){var c=this.prototype.constructor.sup;this.prototype[b]=function(){this._name=b,this.sup=c;return a[b].apply(this,arguments)}}function d(a){for(var b in a)a.hasOwnProperty(b)&&(this[b]=a[b]);return this}function c(a){for(var b in a)a.hasOwnProperty(b)&&e.call(this,a,b);return this}function b(){}typeof module!="undefined"&&module.exports?module.exports=f:a.klass=f}(this)
+!function(a){var b=/xyz/.test(function(){xyz})?/\bsupr\b/:/.*/,c=function(){};this.klass=function(a){var b,d=typeof a=="function"?(b={},a):(b=a,c);return klass.extend.call(d,a)},klass.extend=function(a){var d=this,e,f=typeof a=="function"?(e={},a):(e=a,this),g=function g(){d.apply(this,arguments),f.apply(this,arguments)},h=new c;g.methods=function(a){for(var c in a)h[c]=typeof a[c]=="function"&&typeof d.prototype[c]=="function"&&b.test(a[c])?function(a,b){return function(){this.supr=d.prototype[a];return b.apply(this,arguments)}}(c,a[c]):a[c];g.prototype=h;return this},g.methods.call(g,e).constructor=this,g.extend=arguments.callee,g.prototype.implement=g.statics=function(a){for(var b in a)a.hasOwnProperty(b)&&(this[b]=a[b]);return this};return g},typeof module!="undefined"&&module.exports?module.exports=klass:a.klass=klass}(this)
View
2 make/build.js
@@ -11,4 +11,4 @@ ast = ugly.uglify.ast_squeeze(ast);
var min = ugly.uglify.gen_code(ast);
fs.writeFileSync('./klass.min.js', [header, min].join(''), 'utf-8');
-fs.writeFileSync('./klass.js', [header, file].join(''), 'utf-8');
+fs.writeFileSync('./klass.js', [header, file].join(''), 'utf-8');
View
106 src/klass.js
@@ -1,71 +1,51 @@
-!function (context) {
-
- function noop() {}
-
- function methods(o) {
- for (var k in o) {
- o.hasOwnProperty(k) && wrapper.call(this, o, k);
- }
- return this;
- }
-
- function statics(o) {
- for (var k in o) {
- o.hasOwnProperty(k) && (this[k] = o[k]);
- }
- return this;
- }
-
- function wrapper (o, k) {
- var sup = this.prototype.constructor.sup;
- this.prototype[k] = function () {
- this._name = k;
- this.sup = sup;
- return o[k].apply(this, arguments);
- };
- }
-
- function klass(fn) {
- var o = typeof fn != 'function' && fn;
- fn = (typeof fn == 'function') ? fn : noop;
-
- fn.methods = methods;
- fn.statics = statics;
- fn.extend = extend;
- fn.prototype.implement = implement;
- return o ? fn.methods(o) : fn;
- }
-
- function extend(sub) {
- var sup = this;
+!function(context){
+ var fnTest = /xyz/.test(function(){xyz;}) ? /\bsupr\b/ : /.*/, noop = function(){};
+
+ this.klass = function(o){
+ var methods, _constructor = typeof o == 'function' ? (methods = {}, o) : (methods = o, noop);
+ return klass.extend.call(_constructor, o);
+ };
+
+ klass.extend = function(o) {
+
+ var supr = this,
+ _methods,
+ _constructor = typeof o == 'function' ? (_methods = {}, o) : (_methods = o, this),
+ fn = function fn() {
+ supr.apply(this, arguments);
+ _constructor.apply(this, arguments);
+ };
+
+ var prototype = new noop();
+
+ fn.methods = function (prop) {
+ for (var name in prop) {
+ prototype[name] = typeof prop[name] == "function" &&
+ typeof supr.prototype[name] == "function" && fnTest.test(prop[name]) ?
+ (function(name, fn){
+ return function() {
+ this.supr = supr.prototype[name];
+ return fn.apply(this, arguments);
+ };
+ })(name, prop[name]) :
+ prop[name];
+ }
- function fn() {
- sup.apply(this, arguments);
- typeof sub == 'function' && sub.apply(this, arguments);
+ fn.prototype = prototype;
+ return this;
}
- var F = function (){};
- F.prototype = sup.prototype;
- fn.prototype = new F();
- klass(fn);
-
-
- fn.prototype.constructor = fn;
- fn.prototype.constructor.sup = sup;
- fn.prototype.supr = function () {
- if (this.sup.prototype[this._name]) {
- return this.sup.prototype[this._name].apply(this, arguments);
+ fn.methods.call(fn, _methods).constructor = this;
+ fn.extend = arguments.callee;
+ fn.prototype.implement = fn.statics = function (o) {
+ for (var k in o) {
+ o.hasOwnProperty(k) && (this[k] = o[k]);
}
- };
-
- return (typeof sub != 'function' && sub && fn.methods(sub)) || fn;
- }
-
- function implement(o) {
- this.constructor.methods(o);
- return this;
- }
+ return this;
+ }
+ return fn;
+ };
if (typeof module !== 'undefined' && module.exports) {
module.exports = klass;
View
2 test/test.html
@@ -5,7 +5,7 @@
<title>klass tests</title>
<link rel="stylesheet" href="../build/sink/src/sink.css" type="text/css" media="screen" title="no title" charset="utf-8">
<script src="../build/sink/src/sink.js"></script>
- <script src="../klass.js"></script>
+ <script src="../src/klass.js"></script>
</head>
<body>
<h1>Klass tests</h1>
View
66 test/tests.js
@@ -2,20 +2,21 @@ if (typeof module !== 'undefined' && module.exports) {
var sink = require('../build/sink'),
start = sink.start,
sink = sink.sink;
- var klass = require('../klass');
+ var klass = require('../src/klass');
}
-sink('Klass', function (test, ok, before, after) {
+sink('klass', function (test, ok, before, after) {
- var base;
+
+ var Base;
before(function () {
- base = klass(function (n) {
+ Base = klass(function (n) {
this.n = n;
});
});
- test('should create a base class', 1, function () {
- ok((new base(5).n == 5), 'created base class');
+ test('should create a Base class', 1, function () {
+ ok((new Base(5).n == 5), 'created Base class');
});
test('should allow optional hash as constructor for methods', 1, function () {
@@ -28,73 +29,72 @@ sink('Klass', function (test, ok, before, after) {
this.foo = foo;
return this;
}
-
});
ok((new objectKlass().set('booshr').get() == 'booshr'), 'booshr is found');
});
test('should create methods', 1, function () {
- base.methods({
+ Base.methods({
get: function () {
return this.n;
}
});
- ok((new base('hello').get() == 'hello'), 'created get() method');
+ ok((new Base('hello').get() == 'hello'), 'created get() method');
});
- test('should inherit from superclass when creating subclass', 1, function () {
- var sub = base.extend();
- ok((new sub('boosh').n == 'boosh'), 'inherits property from base');
+ test('should inherit from superclass when creating Subclass', 1, function () {
+ var Sub = Base.extend();
+ ok((new Sub('boosh').n == 'boosh'), 'inherits property from Base');
});
- test('should call super methods from sub methods', 5, function () {
+ test('should call super methods from Sub methods', 5, function () {
var methodTimes = 0;
var constructTimes = 0;
- base
+ Base
.methods({
bah: function () {
- ok(++methodTimes == 1, 'called base method first');
+ ok(++methodTimes == 1, 'called Base method first');
}
});
- var sub1 = base.extend(function() {
- ok(++constructTimes == 1, 'called sub1 constructor first');
+ var Sub1 = Base.extend(function() {
+ ok(++constructTimes == 1, 'called Sub1 constructor first');
})
.methods({
bah: function() {
this.supr();
- ok(++methodTimes == 2, 'called sub1 method second');
+ ok(++methodTimes == 2, 'called Sub1 method second');
}
});
- var sub2 = sub1.extend(function () {
- ok(++constructTimes == 2, 'called sub2 constructor second');
+ var Sub2 = Sub1.extend(function () {
+ ok(++constructTimes == 2, 'called Sub2 constructor second');
})
.methods({
bah: function () {
this.supr();
- ok(++methodTimes == 3, 'called sub2 method third');
+ ok(++methodTimes == 3, 'called Sub2 method third');
}
});
- (new sub2()).bah();
+ (new Sub2()).bah();
});
test('should implement a wrapper method for mixins', 5, function () {
var thingCalled = 0;
- base.methods({
+ Base.methods({
thing: function () {;
- ok(true, 'base thing() gets called');
+ ok(true, 'Base thing() gets called');
}
});
- var sub = base.extend({
+ var Sub = Base.extend({
thing: function () {
this.supr();
ok((++thingCalled == 1), 'calls middleware only once');
}
});
- var inst = new sub('hello');
+ var inst = new Sub('hello');
inst.thing();
@@ -111,6 +111,20 @@ sink('Klass', function (test, ok, before, after) {
});
+ test('should be able to set statics', 1, function () {
+ Base.statics({
+ 'dead': 'obese'
+ });
+ ok(Base.dead == 'obese', 'dead should be a static value of obese')
+ });
+
+ test('should be instances of their classes', 2, function () {
+ var Sub = Base.extend(),
+ b = new Base();
+ s = new Sub();
+ ok(s instanceof Sub, 's is instance of Sub');
+ ok(b instanceof Base, 'b is instance of Base');
+ });
});
start();

0 comments on commit 0f9f6fd

Please sign in to comment.
Something went wrong with that request. Please try again.