Permalink
Browse files

fixes object literal subclassing

adds support for initialize method
adds note about both to readme
  • Loading branch information...
1 parent d354503 commit 95acc7bcd49e97753b8d66334f00bbd0e121b016 @fat fat committed Apr 5, 2011
Showing with 85 additions and 7 deletions.
  1. +24 −0 README.md
  2. +6 −6 src/klass.js
  3. +55 −1 test/tests.js
View
@@ -34,6 +34,30 @@ Interface
new SuperHuman('Zelda').walk()
+<h3>Object Literals...</h3>
+(because sometimes you want to use little curlies ;)
+
+ var Foo = klass({
+
+ foo: 0,
+
+ initialize: function() {
+ this.foo = 1;
+ },
+
+ getFoo: function () {
+ return this.foo;
+ },
+
+ setFoo: function (x) {
+ this.foo = x;
+ return this.getFoo();
+ }
+
+ });
+
+*note: initialize will be called on class invocation*
+
<h3>Implementing...</h3>
(because sometimes you want to overwrite OR mixin an instance method)
View
@@ -12,7 +12,6 @@
return extend.call(typeof o == f ? o : noop, o, 1);
}
-
function wrap(k, fn, supr) {
return function () {
var tmp = this.supr;
@@ -35,6 +34,7 @@
}
function extend(o, fromSub) {
+ noop[proto] = this[proto];
var supr = this,
prototype = new noop(),
isFunction = typeof o == f,
@@ -43,6 +43,9 @@
fn = function () {
fromSub || isFn(o) && supr.apply(this, arguments);
_constructor.apply(this, arguments);
+ if (this.initialize) {
+ this.initialize.apply(this, arguments);
+ }
};
fn.methods = function (o) {
@@ -52,6 +55,7 @@
};
fn.methods.call(fn, _methods).prototype.constructor = fn;
+
fn.extend = arguments.callee;
fn[proto].implement = fn.statics = function (o, optFn) {
o = typeof o == 'string' ? (function () {
@@ -63,10 +67,6 @@
return this;
};
- if (isFunction) {
- process(fn[proto], this.prototype, supr);
- }
-
return fn;
}
@@ -81,4 +81,4 @@
context.klass = klass;
}
-}(this, 'function');
+}(this, 'function');
View
@@ -7,7 +7,6 @@ if (typeof module !== 'undefined' && module.exports) {
sink('klass', function (test, ok, before, after) {
-
var Base;
before(function () {
Base = klass(function (n) {
@@ -297,5 +296,60 @@ sink('klass', function (test, ok, before, after) {
ok(Base.baz() == 'awwwshiii', 'accessed thunk() from baz()');
});
+ test('Object Literal Syntax', 6, function () {
+
+ var Foo = klass({
+
+ foo: 0,
+ bar: 2,
+ baz: 3,
+
+ initialize: function() {
+ this.foo = 1;
+ },
+
+ getFoo: function () {
+ return this.foo;
+ },
+
+ setFoo: function (x) {
+ this.foo = x;
+ return this.getFoo();
+ }
+
+ });
+
+ var Bar = Foo.extend({
+
+ initialize: function () {
+ this.foo = 2;
+ },
+
+ setFoo: function (x) {
+ this.foo = x * 2;
+ return this.getFoo();
+ }
+
+ });
+
+ var first = new Foo(); //normal class
+ var second = new Bar(); //sub class
+ var third = new Bar() //mixin
+ .implement({
+ setFoo: function (x) {
+ this.foo = x * 3;
+ return this.getFoo();
+ }
+ });
+
+ ok(first.getFoo() == 1, 'should have called initialize method');
+ ok(first.setFoo(5) == 5, 'should set instance variables');
+ ok(second.getFoo() == 2, 'should have overridden initialize method');
+ ok(second.setFoo(5) == 10, 'should be 10');
+ ok(third.getFoo() == 2, 'should be 1');
+ ok(third.setFoo(10) == 30, 'should be 10');
+
+ });
+
});
start();

0 comments on commit 95acc7b

Please sign in to comment.