Class Declaration Framework

Most of our framework is encapsulated in a class called XClass. The notation for declaring a class is implemented by the declare method. Here is part of the prototype for XClass containing the declare method. Hover over an area to see a comment explaining the code.

01: XClass.prototype = { 02: constructor: XClass, 03: declare: function(className, superclass, properties) {
04: var f = new Function(); 05: f.prototype = superclass.prototype; 06: var cproto = new f(); 07: cproto.className = className; 08: cproto.superclass = superclass;
09: var ctor = properties['constructor']; 10: if ( ctor ) { 11: var a = [ ctor ]; 12: cproto.constructors = superclass.prototype.constructors.concat(a); 13: } 14: else { 15: cproto.constructors = superclass.constructors; 16: }
17: this.merge(cproto, properties);
18: eval("var newClass = function " + className + 19: "(){this.init(arguments);}");
20: proto.constructor = newClass; 21: newClass.prototype = cproto; 22: window[className] = newClass;
23: } 24: }

Next we examine the merge method.