/* Project: NewClass Version: 2.0.0.0 Author: RabiatoR Mail: @xaker.ru */ /*Вспомогательные функции*/ function call_function( functionName, args, parent ){ var func = typeof( functionName ) === 'function' ? functionName : this.window[functionName]; if( typeof func !== 'function' ){ throw new Error( "call_function: invalid functionName" ); } return func.apply( parent || func, args ); } var MakeClass = function(){ return function( args ){ if( this instanceof arguments.callee ){ if( typeof this.__construct == "function" ){ this.__flag = true; this.__construct.apply( this, args ); this.__flag = false; } }else return new arguments.callee( arguments ); }; } /*Функция создания нового класса*/ var NewClass = function( className, constructor, private, public ){ var retn = MakeClass(); retn.prototype.__class = className; retn.prototype.__construct = constructor; retn.prototype.__private = []; retn.prototype.__public = []; retn.prototype.__flag = false; retn.prototype.__execute = function( func, args ){ if( this.flag ){ if( typeof( this.__private[func] ) === 'function' ) call_function( this.__private[func], args, this ); }else{ if( typeof( this.__public[func] ) === 'function' ){ this.flag = true; call_function( this.__public[func], args, this ); this.flag = false; }else if( typeof( this.__private[func] ) === 'function' ){ throw new Error( this.__class + ": try to call \"" + func + "\" private method." ); } } } retn.prototype.__append = function( key ){ retn.prototype[key] = function (){ retn.prototype.__execute( key, arguments ); }; } for( var key in private ){ retn.prototype.__private[key] = private[key]; retn.prototype.__append( key ); } for( var key in public ){ retn.prototype.__public[key] = public[key]; retn.prototype.__append( key ); } window[className] = retn; return true; } /*Пример создания нового класса*/ NewClass( "MyClass", function(){ alert( "MyClass created!" ); },{ palert:function( txt ){ alert( "private palert( " + txt + " );" ); }, testalert:function( txt ){ alert( "private test alert( " + txt + " );" ); } },{ palert:function( txt ){ this.testalert( txt ); this.palert( txt ); alert( "public palert( " + txt + " );" ); } }); /*Тест работоспособности*/ var g = MyClass(); g.palert( "palert called." ); g.testalert( "testalert called." );