Pass an optional list of static members in the 'extend' call. This method adds static properties (if any) to the "statics" attribute in the constructor function.
Code Changes
Changed as follows. These lines are added immediately after the 'dummy class constructor' code:
if(staticProp) { Class.statics = []; for (var name in staticProp) { !Class.statics[name] && (Class.statics[name] = staticProp[name]); } }
Added an additional argument "staticProp", added during the type declaration, to allow the introduction of static elements at this stage:
Class.extend = function(prop,staticProp) {
The script can be found here , contains some tests.
Examples of using
It can determine the statics at the time of type declaration, using the second optional constructor argument:
var A = Class.extend({},{myStatic:1});
You can access / define the statics inside the instance method:
var B = Class.extend({test:function(){B.statics.myStatic=2;}});
Or an externally instance:
A.statics.myStatic=3;
Example with requirejs:
Put the .js class in the baseUrl folder. An example of a new class definition. It is not necessary to specify the file of the new class in the same way as "var C" (ie C.js), but it is probably better for readability, therefore references to the name C in the methods of the class are aligned with any external links to its static elements :
define(['Class'],function($) { var C = Class.extend({ init: function(params){ C.statics.myStatic++;
Another class in D.js refers to static data in class C:
define(['Class', 'C'],function($,C) { var D = Class.extend({ init: function(params){ C.statics.myStatic++;