Skip to content

Commit

Permalink
Better Function tests and bug fix that would make inherited construct…
Browse files Browse the repository at this point in the history
…ors fail

* Removed support for __definegetter__ use Object.defineProperty if you
must.
* Fixed bug where calling constructors on inherited classes without a
local overload would fail.
* Made class initialization slightly faster
* Made Function tests type strong.
* Updated unit tests to test new cases.
  • Loading branch information
Benjaminsen committed Oct 5, 2013
1 parent c9924eb commit 8132967
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 19 deletions.
4 changes: 2 additions & 2 deletions bin/extend.min.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
ExtendJS 0.2.0
ExtendJS 0.2.1
More info at http://extendjs.org
Copyright (c) 2013 ChrisBenjaminsen.com
Expand All @@ -9,4 +9,4 @@ http://extendjs.org/licence.txt
This notice shall be included in all copies or substantial portions of the Software.
*/
!function(){function a(d){void 0!==d.parent&&(a.apply(this,[d.parent]),this.super=b(this,c(this,this.constructor))),d.apply(this,arguments)}function b(a,b){for(var d in a)"constructor"!=d&&"toString"!=d&&"super"!=d&&!a.__lookupGetter__(d)&&a[d]instanceof Function&&(b[d]=a[d].super||c(a,a[d]));return b}function c(a,b){var c=a.super;return b.super=function(){return a.super=c,b.apply(a,arguments)}}this.Class=function(){},Class.extend=function(c){function d(){a!==arguments[0]&&(a.apply(this,[c]),b(this,this),void 0!==this.initializer&&this.initializer.apply(this),this.constructor.apply(this,arguments))}d.prototype=new this(a),d.prototype.constructor=d,d.toString=function(){return c.toString()};var e=arguments.callee;return d.extend=function(a){return a.parent=c,e.apply(d,arguments)},d},Class=Class.extend(function(){this.constructor=function(){}})}();
!function(){function t(r){r.parent instanceof Function&&(t.apply(this,[r.parent]),this.super=n(this,i(this,this.constructor))),r.apply(this,arguments)}function n(t,n){for(var r in t)"super"!==r&&t[r]instanceof Function&&(n[r]=t[r].super||i(t,t[r]));return n}function i(t,n){var i=t.super;return n.super=function(){return t.super=i,n.apply(t,arguments)}}this.Class=function(){},Class.extend=function(i){function r(){t!==arguments[0]&&(t.apply(this,[i]),n(this,this),this.initializer instanceof Function&&this.initializer.apply(this),this.constructor.apply(this,arguments))}r.prototype=new this(t),r.prototype.constructor=r,r.toString=function(){return""+i};var s=arguments.callee;return r.extend=function(t){return t.parent=i,s.apply(r,arguments)},r},Class=Class.extend(function(){this.constructor=function(){}})}()
26 changes: 14 additions & 12 deletions src/extend.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
ExtendJS 0.2.0
ExtendJS 0.2.1
More info at http://extendjs.org
Copyright (c) 2013 ChrisBenjaminsen.com
Expand All @@ -26,7 +26,7 @@
//Helper method for creating an super copied object clone
function initialize(method){
//Recursivly execute parent methods.
if(method.parent !== undefined){
if(method.parent instanceof Function){
initialize.apply(this,[method.parent]);
this.super = cloneCopy(this,
superCopy(this,this.constructor)
Expand All @@ -38,12 +38,12 @@
//Helper method which allows for super referances.
function cloneCopy(from, to){
for(var x in from){
if(x != "constructor" && x != "toString" && x != "super" && !from.__lookupGetter__(x) && from[x] instanceof Function ){
if(x !== "super" && from[x] instanceof Function){
//Never create circular super referances.
to[x] = from[x].super || superCopy(from, from[x]);
}
}
return to
return to;
}

function superCopy(scope, method){
Expand All @@ -59,20 +59,22 @@
Class.extend = function(to){
function child(){
//Prevent the prototype scope set executing the constructor.
if(initialize === arguments[0]) return;
//Create inhereted object
initialize.apply(this,[to]);
//Setup scope for class instance method calls
cloneCopy(this,this);
if(this.initializer !== undefined) this.initializer.apply(this);
this.constructor.apply(this,arguments);
if(initialize !== arguments[0]){
//Create inhereted object
initialize.apply(this,[to]);
//Setup scope for class instance method calls
cloneCopy(this,this);
if(this.initializer instanceof Function)
this.initializer.apply(this);
this.constructor.apply(this,arguments);
}
}

//Set prototype and constructor enabeling propper type checking.
child.prototype = new this(initialize);
child.prototype.constructor = child;

//Fix tostrings
//Return expected result from toString
child.toString = function(){
return to.toString()
}
Expand Down
4 changes: 2 additions & 2 deletions tests/extend.min.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
ExtendJS 0.2.0
ExtendJS 0.2.1
More info at http://extendjs.org
Copyright (c) 2013 ChrisBenjaminsen.com
Expand All @@ -9,4 +9,4 @@ http://extendjs.org/licence.txt
This notice shall be included in all copies or substantial portions of the Software.
*/
!function(){function a(d){void 0!==d.parent&&(a.apply(this,[d.parent]),this.super=b(this,c(this,this.constructor))),d.apply(this,arguments)}function b(a,b){for(var d in a)"constructor"!=d&&"toString"!=d&&"super"!=d&&!a.__lookupGetter__(d)&&a[d]instanceof Function&&(b[d]=a[d].super||c(a,a[d]));return b}function c(a,b){var c=a.super;return b.super=function(){return a.super=c,b.apply(a,arguments)}}this.Class=function(){},Class.extend=function(c){function d(){a!==arguments[0]&&(a.apply(this,[c]),b(this,this),void 0!==this.initializer&&this.initializer.apply(this),this.constructor.apply(this,arguments))}d.prototype=new this(a),d.prototype.constructor=d,d.toString=function(){return c.toString()};var e=arguments.callee;return d.extend=function(a){return a.parent=c,e.apply(d,arguments)},d},Class=Class.extend(function(){this.constructor=function(){}})}();
!function(){function t(r){r.parent instanceof Function&&(t.apply(this,[r.parent]),this.super=n(this,i(this,this.constructor))),r.apply(this,arguments)}function n(t,n){for(var r in t)"super"!==r&&t[r]instanceof Function&&(n[r]=t[r].super||i(t,t[r]));return n}function i(t,n){var i=t.super;return n.super=function(){return t.super=i,n.apply(t,arguments)}}this.Class=function(){},Class.extend=function(i){function r(){t!==arguments[0]&&(t.apply(this,[i]),n(this,this),this.initializer instanceof Function&&this.initializer.apply(this),this.constructor.apply(this,arguments))}r.prototype=new this(t),r.prototype.constructor=r,r.toString=function(){return""+i};var s=arguments.callee;return r.extend=function(t){return t.parent=i,s.apply(r,arguments)},r},Class=Class.extend(function(){this.constructor=function(){}})}()
14 changes: 11 additions & 3 deletions tests/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@
this.inner = function(){
return "**"
}
this.__defineGetter__("x", function(){
shouldNotBeCalled = false;
});
})





new newClass()
new newClass2()

Expand All @@ -138,5 +138,13 @@
WriteUnitTest(didExecute == "Success", "Testing if we can call constructor via non existing constructor overwrite")//*/
WriteUnitTest(new ChildClass().test() == "||--**", "Secound validation for complex scopes")
WriteUnitTest(shouldNotBeCalled, "Testing that getters are not being called during class cloning")

var bef = new Date().getTime();
for( var a=0;a<100000;a++){
var d = new myClass4()
}
console.log(new Date().getTime()-bef)


</script>
</html>

0 comments on commit 8132967

Please sign in to comment.