пофиксил, ускорил скорость работы) добавил поддержку статических, приватных и даже публичных свойств =)
function Class( description ) {
return Class.extend( description );
}
Class.prototype['constructor'] = function() {};
Class['extend'] = function( description ) {
description = parse( description );
description.prototype = this.prototype;
var prototype = new description( description.prototype, constructor );
var _constructor = prototype['constructor'];
function constructor() {
return _constructor.apply( this, arguments );
}
constructor.prototype = prototype;
constructor['extend'] = this['extend'];
return constructor;
};
function parse( description ) {
var code = description.toString().match( /\{([\s\S]*)\}$/ )[1];
code = parentSupport( code );
code = privateSupport( code );
code = publicSupport( code );
return new Function( 'parent,static', code );
}
function parentSupport( code ) {
var regExp = /\bparent\b\s*\.\s*(\w+)\s*\((.*?)\)/img;
code = code.replace( regExp, function( match, method, params ) {
var separator = /\S/.test( params ) ? ',' : '';
return 'parent.' + method + '.call(this' + separator + params + ')';
} );
return code;
}
function privateSupport( code ) {
var fix = '___';
var regExp = /\bprivate\b.([\w_]+)/img;
code = code.replace( regExp, function( match, prop ) {
return 'this.' + fix + prop + fix;
} );
return code;
}
function publicSupport( code ) {
var regExp = /\bpublic\b.([\w_]+)/img;
code = code.replace( regExp, function( match, prop ) {
return 'this.' + prop;
} );
return code;
}
//######################################################## static
var Cat = new Class( function() {
this.constructor = function() {
static.cnt++
}
static.cnt = 0
} )
new Cat();
new Cat();
new Cat();
alert( Cat.cnt ) // 3
//######################################################## private
var Cat = new Class( function() {
public.name = 'Cat'
private.age = 11;
} );
var q = new Cat();
alert( q.name ); // Cat
alert( q.age ); // undefined