Permalink
Browse files

v2.0.3

  • Loading branch information...
1 parent 1e3ac21 commit 741ec9ce5ff6cf7eeb036aa142c537cbdb47de13 @tnhu committed Mar 15, 2012
Showing with 71 additions and 43 deletions.
  1. +10 −1 CHANGELOG.txt
  2. +18 −2 README.md
  3. +21 −30 jsface.js
  4. +6 −6 test/tests/core.js
  5. +16 −4 test/tests/pointcut.js
View
@@ -1,7 +1,16 @@
-v2.0.3 TBD
+v2.1.0 TBD
----------------------------------------------------------------
* Add overloading plugin: support method overloading, type
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
----------------------------------------------------------------
View
@@ -8,6 +8,20 @@ JSFace will give you a good taste on your JavaScript OOP development.
* Support singleton, mixin, private properties, Aspect Oriented Programming.
* 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
JSFace supports both server side (CommonJS) and client side JavaScript (browser).
@@ -64,11 +78,13 @@ person.toString(); // "Rika/20"
var Student = Class(Person, {
constructor: function(id, name, age) {
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() {
- 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
}
});
View
@@ -4,7 +4,7 @@
*
* Copyright (c) 2009-2012 Tan Nhu
* 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) {
/**
@@ -83,36 +83,26 @@
* Make $super method.
*/
function makeSuper(child, parent) {
- child.prototype.$super = function() {
- var caller = arguments.callee.caller,
- supez = this.$super,
- result, name, len, func, pa;
-
- if (caller === child.prototype.constructor) { // caller is constructor
- func = parent ? parent[0] : 0;
- if (isClass(func)) { this.$super = func.prototype.$super; } // $super = parent.$super
- } else if (parent) { // caller is a method: query its name
- 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;
+ parent = parent && parent[0];
+
+ var iClass = isClass(parent), proto = child.prototype, __super;
+
+ if (parent) {
+ each(proto, function(fnName, fn) {
+ __super = fnName !== "$super" && isFunction(fn) && ( (iClass && isFunction(parent.prototype[fnName]) && parent.prototype[fnName]) || (!iClass && isFunction(parent[fnName]) && parent[fnName]));
+ if (__super) {
+ fn.__super = __super;
}
+ });
- 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
- result = func.apply(this, arguments); // execute matched super method
- this.$super = supez; // restore $super
- return result;
- }
+ proto.$super = function $super() {
+ var __super = $super.caller.__super; // VERY EXPENSIVE!!!
+ return __super && __super.apply(this, arguments);
};
}
@@ -157,7 +147,8 @@
api = api || {};
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
api = isFunction(api) ? api() : api; // execute api if it's a function
@@ -169,7 +160,7 @@
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
extend(clazz, p, ignoredKeys, 1);
View
@@ -292,11 +292,11 @@ test("Create a sub class", function() {
var Bar = Class(Foo, {
constructor: function(name) {
- this.$super(name);
+ Bar.$super.call(this, name);
},
sayHi: function() {
- return this.$super();
+ return Bar.$superp.sayHi.call(this);
},
sayBye: function() {
@@ -325,21 +325,21 @@ test("Multiple level class inheritance", function() {
var Bar = Class(Foo, {
constructor: function(name) {
- this.$super(name);
+ Bar.$super.call(this, name);
},
sayHi: function() {
- return this.$super();
+ return Bar.$superp.sayHi.call(this);
}
});
var Child = Class(Bar, {
constructor: function(name) {
- this.$super(name);
+ Child.$super.call(this, name);
},
sayHi: function() {
- return this.$super();
+ return Child.$superp.sayHi.call(this);
}
});
View
@@ -420,19 +420,31 @@ test("Pointcuts and $super", function() {
Child = pointcut(Child, {
constructor: {
- before: function() {},
- after: function() {}
+ before: function() {
+ this.before = true;
+ },
+ after: function() {
+ this.after = true;
+ }
},
inc: {
- before: function() {},
- after: function() {}
+ before: function() {
+ this.iBefore = true;
+ },
+ after: function() {
+ this.iAfter = true;
+ }
}
});
var p = new Child(100);
p.inc(1);
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() {

0 comments on commit 741ec9c

Please sign in to comment.