Skip to content

Commit 8132967

Browse files
committed
Better Function tests and bug fix that would make inherited constructors 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.
1 parent c9924eb commit 8132967

File tree

4 files changed

+29
-19
lines changed

4 files changed

+29
-19
lines changed

bin/extend.min.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
ExtendJS 0.2.0
2+
ExtendJS 0.2.1
33
More info at http://extendjs.org
44
55
Copyright (c) 2013 ChrisBenjaminsen.com
@@ -9,4 +9,4 @@ http://extendjs.org/licence.txt
99
1010
This notice shall be included in all copies or substantial portions of the Software.
1111
*/
12-
!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(){}})}();
12+
!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(){}})}()

src/extend.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
ExtendJS 0.2.0
2+
ExtendJS 0.2.1
33
More info at http://extendjs.org
44
55
Copyright (c) 2013 ChrisBenjaminsen.com
@@ -26,7 +26,7 @@
2626
//Helper method for creating an super copied object clone
2727
function initialize(method){
2828
//Recursivly execute parent methods.
29-
if(method.parent !== undefined){
29+
if(method.parent instanceof Function){
3030
initialize.apply(this,[method.parent]);
3131
this.super = cloneCopy(this,
3232
superCopy(this,this.constructor)
@@ -38,12 +38,12 @@
3838
//Helper method which allows for super referances.
3939
function cloneCopy(from, to){
4040
for(var x in from){
41-
if(x != "constructor" && x != "toString" && x != "super" && !from.__lookupGetter__(x) && from[x] instanceof Function ){
41+
if(x !== "super" && from[x] instanceof Function){
4242
//Never create circular super referances.
4343
to[x] = from[x].super || superCopy(from, from[x]);
4444
}
4545
}
46-
return to
46+
return to;
4747
}
4848

4949
function superCopy(scope, method){
@@ -59,20 +59,22 @@
5959
Class.extend = function(to){
6060
function child(){
6161
//Prevent the prototype scope set executing the constructor.
62-
if(initialize === arguments[0]) return;
63-
//Create inhereted object
64-
initialize.apply(this,[to]);
65-
//Setup scope for class instance method calls
66-
cloneCopy(this,this);
67-
if(this.initializer !== undefined) this.initializer.apply(this);
68-
this.constructor.apply(this,arguments);
62+
if(initialize !== arguments[0]){
63+
//Create inhereted object
64+
initialize.apply(this,[to]);
65+
//Setup scope for class instance method calls
66+
cloneCopy(this,this);
67+
if(this.initializer instanceof Function)
68+
this.initializer.apply(this);
69+
this.constructor.apply(this,arguments);
70+
}
6971
}
7072

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

75-
//Fix tostrings
77+
//Return expected result from toString
7678
child.toString = function(){
7779
return to.toString()
7880
}

tests/extend.min.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
ExtendJS 0.2.0
2+
ExtendJS 0.2.1
33
More info at http://extendjs.org
44
55
Copyright (c) 2013 ChrisBenjaminsen.com
@@ -9,4 +9,4 @@ http://extendjs.org/licence.txt
99
1010
This notice shall be included in all copies or substantial portions of the Software.
1111
*/
12-
!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(){}})}();
12+
!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(){}})}()

tests/test.html

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,12 @@
108108
this.inner = function(){
109109
return "**"
110110
}
111-
this.__defineGetter__("x", function(){
112-
shouldNotBeCalled = false;
113-
});
114111
})
115112

116113

114+
115+
116+
117117
new newClass()
118118
new newClass2()
119119

@@ -138,5 +138,13 @@
138138
WriteUnitTest(didExecute == "Success", "Testing if we can call constructor via non existing constructor overwrite")//*/
139139
WriteUnitTest(new ChildClass().test() == "||--**", "Secound validation for complex scopes")
140140
WriteUnitTest(shouldNotBeCalled, "Testing that getters are not being called during class cloning")
141+
142+
var bef = new Date().getTime();
143+
for( var a=0;a<100000;a++){
144+
var d = new myClass4()
145+
}
146+
console.log(new Date().getTime()-bef)
147+
148+
141149
</script>
142150
</html>

0 commit comments

Comments
 (0)