Skip to content

Commit

Permalink
v2.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
tnhu committed Mar 15, 2012
1 parent 1e3ac21 commit 741ec9c
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 43 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.txt
Original file line number Original file line Diff line number Diff line change
@@ -1,7 +1,16 @@
v2.0.3 TBD v2.1.0 TBD
---------------------------------------------------------------- ----------------------------------------------------------------
* Add overloading plugin: support method overloading, type * Add overloading plugin: support method overloading, type
checking, arguments validation checking, arguments validation
* Remove $super(): One way to rule them all
* Use "use strict"
* Optimize speed and add benchmarks

v2.0.3 TBD
----------------------------------------------------------------
* Introduce Child.$super, Child.$superp
* Deprecate $super(): hate to see it's slow vs. other frameworks
* Update unit tests (use Child.$super, Child.$superp)


v2.0.2 Mar 09, 2012 v2.0.2 Mar 09, 2012
---------------------------------------------------------------- ----------------------------------------------------------------
Expand Down
20 changes: 18 additions & 2 deletions README.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ JSFace will give you a good taste on your JavaScript OOP development.
* Support singleton, mixin, private properties, Aspect Oriented Programming. * Support singleton, mixin, private properties, Aspect Oriented Programming.
* Plugins mechanism to extend itself. * Plugins mechanism to extend itself.


# Changelog (v2.0.3)

* Introduce Child.$super, Child.$superp poiting to Parent and Parent.prototype
* Deprecate $super(): hate to see JSFace's slow vs. other frameworks

See all [changlogs](https://github.com/tannhu/jsface/blob/master/CHANGELOG.txt).

# Next version (v2.1.0)

* Remove $super(): Use Child.$super and Child.$superp to gain more performance
* Add overloading plugin: support method overloading, type checking, arguments validation
* Use "use strict"
* More speed optimization and benchmark

# Setup # Setup


JSFace supports both server side (CommonJS) and client side JavaScript (browser). JSFace supports both server side (CommonJS) and client side JavaScript (browser).
Expand Down Expand Up @@ -64,11 +78,13 @@ person.toString(); // "Rika/20"
var Student = Class(Person, { var Student = Class(Person, {
constructor: function(id, name, age) { constructor: function(id, name, age) {
this.id = id; this.id = id;
this.$super(name, age); // Invoke parent's constructor Student.$super.call(this, name, age); // Invoke parent's constructor
// this.$super(name, age); // This api will be removed in v2.1.0
}, },


toString: function() { toString: function() {
return this.id + "/" + this.$super(); // Invoke parent's toString method return this.id + "/" + Student.$superp.toString.call(this); // Invoke parent's toString method
// return this.id + "/" + this.$super(); // This api will be removed in v2.1.0
} }
}); });


Expand Down
51 changes: 21 additions & 30 deletions jsface.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* *
* Copyright (c) 2009-2012 Tan Nhu * Copyright (c) 2009-2012 Tan Nhu
* Licensed under MIT license (https://github.com/tannhu/jsface/blob/master/MIT-LICENSE.txt) * Licensed under MIT license (https://github.com/tannhu/jsface/blob/master/MIT-LICENSE.txt)
* Version: 2.0.2 * Version: 2.0.3
*/ */
(function(context, OBJECT, NUMBER, LENGTH, INVALID, undefined, oldClass, jsface) { (function(context, OBJECT, NUMBER, LENGTH, INVALID, undefined, oldClass, jsface) {
/** /**
Expand Down Expand Up @@ -83,36 +83,26 @@
* Make $super method. * Make $super method.
*/ */
function makeSuper(child, parent) { function makeSuper(child, parent) {
child.prototype.$super = function() { parent = parent && parent[0];
var caller = arguments.callee.caller,
supez = this.$super, var iClass = isClass(parent), proto = child.prototype, __super;
result, name, len, func, pa;

if (parent) {
if (caller === child.prototype.constructor) { // caller is constructor each(proto, function(fnName, fn) {
func = parent ? parent[0] : 0; __super = fnName !== "$super" && isFunction(fn) && ( (iClass && isFunction(parent.prototype[fnName]) && parent.prototype[fnName]) || (!iClass && isFunction(parent[fnName]) && parent[fnName]));
if (isClass(func)) { this.$super = func.prototype.$super; } // $super = parent.$super if (__super) {
} else if (parent) { // caller is a method: query its name fn.__super = __super;
each(child.prototype, function(key, fn) {
if (fn === caller || caller === fn.___origin_fn___) { // pointcut compatible
name = key; // found you
return 1/0; // break each loop
}
});

for (len = parent.length; len-- && !func;) { // order: most right parent first
pa = parent[len]; // pa could be an instance
if (isClass(pa)) { pa = pa.prototype; } // or a class
func = pa[name] || 0;
} }
});


if (isFunction(func)) { this.$super = pa.$super; } // $super = parent.$super child.$super = parent;
} child.__super = parent;
child.$superp = iClass ? parent.prototype : parent;
}


if (isFunction(func)) { // no harm, not found? fine proto.$super = function $super() {
result = func.apply(this, arguments); // execute matched super method var __super = $super.caller.__super; // VERY EXPENSIVE!!!
this.$super = supez; // restore $super return __super && __super.apply(this, arguments);
return result;
}
}; };
} }


Expand Down Expand Up @@ -157,7 +147,8 @@
api = api || {}; api = api || {};


var clazz, constructor, singleton, statics, var clazz, constructor, singleton, statics,
ignoredKeys = { constructor: 1, $singleton: 1, $statics: 1, prototype: 1 }; ignoredKeys = { constructor: 1, $singleton: 1, $statics: 1, prototype: 1 },
overload = Class.overload || function(name, fn){ return fn; };


parent = (parent && !isArray(parent)) ? [ parent ] : parent; // convert to array parent = (parent && !isArray(parent)) ? [ parent ] : parent; // convert to array
api = isFunction(api) ? api() : api; // execute api if it's a function api = isFunction(api) ? api() : api; // execute api if it's a function
Expand All @@ -169,7 +160,7 @@


each(Class.plugins, function(key) { ignoredKeys[key] = 1; }); // add plugins' keys into ignoredKeys each(Class.plugins, function(key) { ignoredKeys[key] = 1; }); // add plugins' keys into ignoredKeys


clazz = singleton ? {} : (constructor ? (Class.overload ? Class.overload("constructor", constructor) : constructor) : function(){}); clazz = singleton ? {} : (constructor ? overload("constructor", constructor) : function(){});


each(parent, function(p) { // extend parent static properties each(parent, function(p) { // extend parent static properties
extend(clazz, p, ignoredKeys, 1); extend(clazz, p, ignoredKeys, 1);
Expand Down
12 changes: 6 additions & 6 deletions test/tests/core.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -292,11 +292,11 @@ test("Create a sub class", function() {


var Bar = Class(Foo, { var Bar = Class(Foo, {
constructor: function(name) { constructor: function(name) {
this.$super(name); Bar.$super.call(this, name);
}, },


sayHi: function() { sayHi: function() {
return this.$super(); return Bar.$superp.sayHi.call(this);
}, },


sayBye: function() { sayBye: function() {
Expand Down Expand Up @@ -325,21 +325,21 @@ test("Multiple level class inheritance", function() {


var Bar = Class(Foo, { var Bar = Class(Foo, {
constructor: function(name) { constructor: function(name) {
this.$super(name); Bar.$super.call(this, name);
}, },


sayHi: function() { sayHi: function() {
return this.$super(); return Bar.$superp.sayHi.call(this);
} }
}); });


var Child = Class(Bar, { var Child = Class(Bar, {
constructor: function(name) { constructor: function(name) {
this.$super(name); Child.$super.call(this, name);
}, },


sayHi: function() { sayHi: function() {
return this.$super(); return Child.$superp.sayHi.call(this);
} }
}); });


Expand Down
20 changes: 16 additions & 4 deletions test/tests/pointcut.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -420,19 +420,31 @@ test("Pointcuts and $super", function() {


Child = pointcut(Child, { Child = pointcut(Child, {
constructor: { constructor: {
before: function() {}, before: function() {
after: function() {} this.before = true;
},
after: function() {
this.after = true;
}
}, },
inc: { inc: {
before: function() {}, before: function() {
after: function() {} this.iBefore = true;
},
after: function() {
this.iAfter = true;
}
} }
}); });


var p = new Child(100); var p = new Child(100);
p.inc(1); p.inc(1);


equal(p.num, 101, "Pointcut works incorrectly with $super"); equal(p.num, 101, "Pointcut works incorrectly with $super");
equal(true, p.before, "Pointcut works incorrectly with $super");
equal(true, p.after, "Pointcut works incorrectly with $super");
equal(true, p.iBefore, "Pointcut works incorrectly with $super");
equal(true, p.iAfter, "Pointcut works incorrectly with $super");
}); });


test("Remove all pointcuts", function() { test("Remove all pointcuts", function() {
Expand Down

0 comments on commit 741ec9c

Please sign in to comment.