Browse files

Merge branch 'master' of github.com:polvero/klass

Conflicts:
	src/klass.js
	test/tests.js
  • Loading branch information...
2 parents 1910508 + d9cb457 commit b953cb891dd073ed301c5ad9dc29bd0cb4e2b300 @fat fat committed Mar 17, 2011
Showing with 178 additions and 47 deletions.
  1. +9 −8 README.md
  2. +42 −17 klass.js
  3. +3 −2 klass.min.js
  4. +3 −3 package.json
  5. +2 −1 src/copyright.js
  6. +15 −9 src/klass.js
  7. +104 −7 test/tests.js
View
17 README.md
@@ -4,7 +4,7 @@ An expressive, cross platform JavaScript Class provider with a slick, classical
Interface
---------
-creating a Class...
+<h3>creating a Class...</h3>
var Person = klass(function (name) {
this.name = name;
@@ -17,7 +17,7 @@ creating a Class...
walk: function () {}
});
-Subclassing...
+<h3>Subclassing...</h3>
var SuperHuman = Person.extend(function (name) {
// super class is automagically called
@@ -34,7 +34,7 @@ Subclassing...
new SuperHuman('Zelda').walk()
-Implementing...
+<h3>Implementing...</h3>
(because sometimes you want to overwrite OR mixin an instance method)
// note you can optionally pass an object literal to extend too ;)
@@ -59,7 +59,7 @@ Environments
------------
Klass is [Common JS](http://commonjs.org) compliant and provides the [Modules 1.1](http://wiki.commonjs.org/wiki/Modules/1.1) interface to allow two flavors of development. See the implementations below:
-browser environment
+<h3>browser environment</h3>
<script src="path/to/klass.js"></script>
<!-- klass() is exposed to context -->
@@ -70,7 +70,7 @@ browser environment
Bar.implement({ ... });
</script>
-as a module
+<h3>as a module</h3>
// your-application.js
var klass = require('path/to/klass');
@@ -85,7 +85,7 @@ If you want to see shiny passing tests, run the _tests_ make command
Install the Package!
--------------------
-By far the easiest way to get started with klass is to simply install the package and hit the ground running!
+By far the easiest way to get started with klass is to simply install the package and hit the ground running!!
% npm install klass
@@ -94,5 +94,6 @@ By far the easiest way to get started with klass is to simply install the packag
Contributors
------------
- * [Dustin Diaz](https://github.com/polvero)
- * [Jacob Thornton](https://github.com/jacobthornton)
+ * [Dustin Diaz](https://github.com/polvero/klass/commits/master?author=polvero)
+ * [Jacob Thornton](https://github.com/polvero/klass/commits/master?author=jacobthornton)
+ * Follow our Software [@dedfat](http://twitter.com/dedfat)
View
59 klass.js
@@ -1,54 +1,79 @@
/**
- * Klass.js - copyright @ded & @fat
+ * Klass.js - copyright @dedfat
* https://github.com/polvero/klass
+ * Follow our software http://twitter.com/dedfat
* MIT License
*/
!function(context){
- var fnTest = /xyz/.test(function(){xyz;}) ? /\bsupr\b/ : /.*/, noop = function(){};
+ var fnTest = /xyz/.test(function (){xyz;}) ? /\bsupr\b/ : /.*/,
+ noop = function(){}, proto = 'prototype',
+ isFn = function (o) {
+ return typeof o === 'function';
+ };
function klass(o){
- var methods, _constructor = typeof o == 'function' ? (methods = {}, o) : (methods = o, noop);
- return extend.call(_constructor, o);
+ var methods, _constructor = isFn(o) ? (methods = {}, o) : (methods = o, noop);
+ return extend.call(_constructor, o, 1);
};
- function extend(o) {
-
+ function extend(o, fromSub) {
+ o = o || noop;
var supr = this,
_methods,
- _constructor = typeof o == 'function' ? (_methods = {}, o) : (_methods = o, this),
- fn = function fn() {
- supr.apply(this, arguments);
+ _constructor = isFn(o) ? (_methods = {}, o) : (_methods = o, this),
+ fn = function () {
+ fromSub || isFn(o) && supr.apply(this, arguments);
_constructor.apply(this, arguments);
- };
-
- var prototype = new noop();
+ },
+ 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]) ?
+ prototype[name] = isFn(prop[name]) &&
+ isFn(supr[proto][name]) && fnTest.test(prop[name]) ?
(function(name, fn){
return function() {
- this.supr = supr.prototype[name];
+ this.supr = supr[proto][name];
return fn.apply(this, arguments);
};
})(name, prop[name]) :
prop[name];
}
- fn.prototype = prototype;
+ fn[proto] = prototype;
return this;
}
fn.methods.call(fn, _methods).constructor = this;
fn.extend = arguments.callee;
- fn.prototype.implement = fn.statics = function (o) {
+
+ if (!fromSub) {
+ for (var key in supr.prototype) {
+ fn[proto][key] = supr.prototype[key];
+ }
+ }
+
+ fn.statics = function (o) {
for (var k in o) {
o.hasOwnProperty(k) && (this[k] = o[k]);
}
return this;
+ };
+
+ function implement(name, f) {
+ return function () {
+ f.apply(this, arguments);
+ return this;
+ };
}
+ fn[proto].implement = function (ob) {
+ for (var k in ob) {
+ this[k] = implement(k, ob[k]);
+ }
+ return this;
+ };
+
return fn;
};
View
5 klass.min.js
@@ -1,6 +1,7 @@
/**
- * Klass.js - copyright @ded & @fat
+ * Klass.js - copyright @dedfat
* https://github.com/polvero/klass
+ * Follow our software http://twitter.com/dedfat
* MIT License
*/
-!function(a){function e(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}function d(a){var b,d=typeof a=="function"?(b={},a):(b=a,c);return e.call(d,a)}var b=/xyz/.test(function(){xyz})?/\bsupr\b/:/.*/,c=function(){};typeof module!="undefined"&&module.exports?module.exports=d:a.klass=d}(this)
+!function(a){function g(a,f){function m(a,b){return function(){b.apply(this,arguments);return this}}a=a||c;var g=this,h,i=e(a)?(h={},a):(h=a,this),j=function(){f||e(a)&&g.apply(this,arguments),i.apply(this,arguments)},k=new c;j.methods=function(a){for(var c in a)k[c]=e(a[c])&&e(g[d][c])&&b.test(a[c])?function(a,b){return function(){this.supr=g[d][a];return b.apply(this,arguments)}}(c,a[c]):a[c];j[d]=k;return this},j.methods.call(j,h).constructor=this,j.extend=arguments.callee;if(!f)for(var l in g.prototype)j[d][l]=g.prototype[l];j.statics=function(a){for(var b in a)a.hasOwnProperty(b)&&(this[b]=a[b]);return this},j[d].implement=function(a){for(var b in a)this[b]=m(b,a[b]);return this};return j}function f(a){var b,d=e(a)?(b={},a):(b=a,c);return g.call(d,a,1)}var b=/xyz/.test(function(){xyz})?/\bsupr\b/:/.*/,c=function(){},d="prototype",e=function(a){return typeof a=="function"};typeof module!="undefined"&&module.exports?module.exports=f:a.klass=f}(this)
View
6 package.json
@@ -1,16 +1,16 @@
{
"name": "klass",
"description": "JavaScript Class provider designed to provide a classical interface to prototypal inheritance",
- "version": "0.0.3",
+ "version": "0.0.8",
"homepage": "http://dustindiaz.com/klass",
"authors": ["Dustin Diaz", "Jacob Thornton"],
"repository": {
"type": "git",
"url": "http://github.com/polvero/klass.git"
},
- "author": "Dustin Diaz <polvero@gmail.com> (http://dustindiaz.com)",
+ "author": "Dustin Diaz / Jacob Thornton (http://twitter.com/dedfat)",
"main": "klass.js",
"engines": {
- "node": "0.0.1"
+ "node": ">= 0.0.1"
}
}
View
3 src/copyright.js
@@ -1,5 +1,6 @@
/**
- * Klass.js - copyright @ded & @fat
+ * Klass.js - copyright @dedfat
* https://github.com/polvero/klass
+ * Follow our software http://twitter.com/dedfat
* MIT License
*/
View
24 src/klass.js
@@ -1,15 +1,20 @@
!function(context){
- var fnTest = /xyz/.test(function(){xyz;}) ? /\bsupr\b/ : /.*/, noop = function(){};
+ var fnTest = /xyz/.test(function (){xyz;}) ? /\bsupr\b/ : /.*/,
+ noop = function(){},
+ proto = 'prototype',
+ isFn = function (o) {
+ return typeof o === 'function';
+ };
function klass(o) {
- return extend.call(typeof o == 'function' ? o : noop, o);
+ return extend.call(typeof o == 'function' ? o : noop, o, 1);
}
function process(what, o, supr){
for (var k in o) {
if (o.hasOwnProperty(k)) {
what[k] = typeof o[k] == "function"
- && typeof supr.prototype[k] == "function"
+ && typeof supr[proto][k] == "function"
&& fnTest.test(o[k])
? wrap(k, o[k], supr) : o[k];
}
@@ -19,37 +24,38 @@
function wrap(k, fn, supr) {
return function () {
var tmp = this.supr;
- this.supr = supr.prototype[k];
+ this.supr = supr[proto][k];
var ret = fn.apply(this, arguments);
this.supr = tmp;
return ret;
};
}
- function extend(o) {
-
+ function extend(o, fromSub) {
var supr = this,
prototype = new noop(),
isFunction = typeof o == 'function',
_constructor = isFunction ? o : this,
_methods = isFunction ? {} : o,
fn = function () {
- supr.apply(this, arguments);
+ fromSub || isFn(o) && supr.apply(this, arguments);
_constructor.apply(this, arguments);
};
fn.methods = function (o) {
process(prototype, o, supr);
- fn.prototype = prototype;
+ fn[proto] = prototype;
return this;
};
fn.methods.call(fn, _methods).constructor = this;
fn.extend = arguments.callee;
+
fn.prototype.implement = fn.statics = function (o) {
process(this, o, supr);
return this;
};
+
return fn;
}
@@ -59,4 +65,4 @@
context.klass = klass;
}
-}(this);
+}(this);
View
111 test/tests.js
@@ -15,6 +15,23 @@ sink('klass', function (test, ok, before, after) {
});
});
+ test('should not call constructor twice', 2, function () {
+ var called = 0;
+ var thing = klass(function () {
+ ok(++called == 1, 'constructor called only once');
+ if (called == 2) {
+ clearTimeout(timer);
+ }
+ });
+
+ new thing();
+
+ var timer = setTimeout(function () {
+ ok(true, 'second constructor never called');
+ }, 200);
+
+ });
+
test('should create a Base class', 1, function () {
ok((new Base(5).n == 5), 'created Base class');
});
@@ -87,30 +104,70 @@ sink('klass', function (test, ok, before, after) {
}
});
- var Sub = Base.extend({
- thing: function () {
- this.supr();
- ok((++thingCalled == 1), 'calls middleware only once');
- }
- });
+ var Sub = Base.extend().methods({
+ thing: function () {
+ console.log('middleware thing()');
+ this.supr();
+ ok((++thingCalled == 1), 'calls middleware only once');
+ }
+ });
var inst = new Sub('hello');
inst.thing();
inst.implement({
thing: function (n) {
- this.supr();
+ console.log('thing()');
ok(true, 'called implementer');
+ this.supr();
this.booooshr();
},
booooshr: function () {
+ console.log('booshr()');
ok(true, 'called booshr');
}
}).thing();
});
+
+ // test('same thing but extend taking object literal', 5, function () {
+ // var thingCalled = 0;
+ // Base.methods({
+ // thing: function () {;
+ // console.log('base thing()');
+ // ok(true, 'Base thing() gets called');
+ // }
+ // });
+ //
+ // var Sub = Base.extend({
+ // thing: function () {
+ // console.log('middleware thing()');
+ // this.supr();
+ // ok((++thingCalled == 1), 'calls middleware only once');
+ // }
+ // });
+ //
+ // var inst = new Sub('hello');
+ //
+ // inst.thing();
+ //
+ // inst.implement({
+ // thing: function (n) {
+ // console.log('thing()');
+ // ok(true, 'called implementer');
+ // this.supr();
+ // this.booooshr();
+ // },
+ // booooshr: function () {
+ // console.log('booshr()');
+ // ok(true, 'called booshr');
+ // }
+ // }).thing();
+ //
+ // });
+
test('should be able to set statics', 1, function () {
Base.statics({
'dead': 'obese'
@@ -153,5 +210,45 @@ sink('klass', function (test, ok, before, after) {
});
+ test('base constructor not called twice when no constructor used in sub', 2, function () {
+ var called = 0;
+ var Base = klass(function () {
+ ok(++called == 1, 'called only once');
+ if (called > 1) {
+ clearTimeout(timer);
+ }
+ });
+
+ var Sub = Base.extend();
+
+ new Sub();
+ var timer = setTimeout(function () {
+ ok(true, 'didnt call base twice');
+ }, 50);
+ });
+
+ test('base constructor not called twice when object used in sub', 2, function () {
+ var called = 0;
+ var Base = klass(function () {
+ ok(++called == 1, 'called only once');
+ if (called > 1) {
+ clearTimeout(timer);
+ timer = null;
+ }
+ });
+
+ var timer = setTimeout(function () {
+ ok(true, 'didnt call base twice');
+ }, 100);
+
+
+ var Sub = Base.extend({
+ thing: function () { },
+ });
+
+ new Sub();
+ });
+
+
});
start();

0 comments on commit b953cb8

Please sign in to comment.