我有一些处理用户操作的模块——它们是控制模块。有 6 个,现在有 5 个,因为我将 2 个非常相似的合并到一个名为 Control 的模块中
我注意到的是,当我整合类似的代码时......它变得有点效率低下。例如,在 Control 中,我现在有一个额外的逻辑语句来确定程序流程。
if( this.type === 'signup' && !this.text_object.checkPattern( 'name' ) )
这个简单的行允许我将 ControlSignIn 和 ControlSignUp 结合起来,因为唯一的区别是检查 Sign Up 中没有 Sign In 的名称。
我可以以这种方式继续,我得到更复杂的代码,但我的代码占用空间更小。
在(复杂性和运行时间)与(代码足迹)之间存在权衡。
我猜这没关系,但我只是想确定一下。
作为一个例子ControlTweet
,我也可以适应Control.
问题?
我应该将 ControlTweet 与 Control 合并吗?
一般来说,你在哪里画线,还是一个偏好问题?
控制
/**
*Control - receives incoming requests for client use
*/
var Control = ( function ()
{
var Control = function ( type )
{
this.TIME = 4000;
this.type = type;
this.form_element = document.getElementById( type ),
this.response_element = document.getElementById( type + '_response' );
this.text_object = new TextValidator( this.form_element ),
this.message_object = new Message( this.response_element ),
this.effects_object= new Effects( this.response_element );
};
Control.prototype.invoke = function( )
{
if( Global.validate_input_on === 1 )
{
if( !this.text_object.checkEmpty() )
{
this.message_object.display( 'empty' );
this.effects_object.fade( 'down', this.TIME );
return false;
}
if( this.type === 'signup' && !this.text_object.checkPattern( 'name' ) )
{
this.message_object.display( 'name' );
this.effects_object.fade( 'down', this.TIME );
return false;
}
if( !this.text_object.checkPattern( 'email' ) )
{
this.message_object.display( 'email' );
this.effects_object.fade( 'down', this.TIME );
return false;
}
if( !this.text_object.checkPattern( 'pass' ) )
{
this.message_object.display( 'pass' );
this.effects_object.fade( 'down', this.TIME );
return false;
}
}
var response_element = this.response_element;
new Ajax().invoke( serializeArray( this.form_element ) + '&ajax_type=' + this.type + '_control', function( server_response_text ) { ajaxType( server_response_text, response_element, 'respond' ); } );
};
Control.in = function()
{
new Control( 'signin' ).invoke();
};
Control.up = function()
{
new Control( 'signup' ).invoke();
};
Control.out = function()
{
new Ajax().invoke( '&ajax_type=ControlSignOut', function( server_response_text ) { ajaxType( server_response_text, 0, 'simple' ); } );
};
Control.try = function()
{
new Ajax().invoke( '&ajax_type=ControlTryIt', function( server_response_text ) { ajaxType( server_response_text, 0, 'simple' ); } );
};
return Control;
} () );
控制推文
/**
* ControlTweet
*/
function interfaceTweet()
{
var fill_element = document.getElementById( 'tweet_fill' ),
form_element = document.getElementById( 'tweet' ),
response_element = document.getElementById( 'tweet_response' );
var text_object = new TextValidator( form_element ),
message_object = new Message( response_element ),
effects_object = new Effects( response_element );
if( Global.validate_input_on === 1 )
{
if( !text_object.checkEmpty() )
{
message_object.display( 'empty' );
effects_object.fade( 'down', 4000 );
return;
}
if( !text_object.checkPattern( 'tweet' ) )
{
message_object.display( 'tweet' );
effects_object.fade( 'down', 4000 );
return;
}
}
new Ajax().invoke( serializeArray( form_element ) + '&ajax_type=ControlTweet_add', function( server_response_text ) { ajaxType( server_response_text, response_element, 'tweet', fill_element ); } );
}
我最终使用的是:
/**
*Control - receives incoming requests for client use
*/
var Control = ( function ()
{
var Control = function ( type )
{
this.TIME = 4000;
this.type = type;
this.form_element = document.getElementById( type ),
this.response_element = document.getElementById( type + '_response' );
if( type === 'tweet' ) { this.fill_element = document.getElementById( type + '_fill' ); }
this.text_object = new TextValidator( this.form_element ),
this.message_object = new Message( this.response_element ),
this.effects_object= new Effects( this.response_element );
};
Control.prototype.invoke = function( )
{
if( Global.validate_input_on === 1 )
{
if( !this.text_object.checkEmpty() )
{
this.message_object.display( 'empty' );
this.effects_object.fade( 'down', this.TIME );
return false;
}
switch( this.type )
{
case 'signin':
if( !this.text_object.checkPattern( 'email' ) )
{
this.message_object.display( 'email' );
this.effects_object.fade( 'down', this.TIME );
return false;
}
if( !this.text_object.checkPattern( 'pass' ) )
{
this.message_object.display( 'pass' );
this.effects_object.fade( 'down', this.TIME );
return false;
}
break;
case 'signup':
if( !this.text_object.checkPattern( 'email' ) )
{
this.message_object.display( 'email' );
this.effects_object.fade( 'down', this.TIME );
return false;
}
if( !this.text_object.checkPattern( 'name' ) )
{
this.message_object.display( 'name' );
this.effects_object.fade( 'down', this.TIME );
return false;
}
if( !this.text_object.checkPattern( 'pass' ) )
{
this.message_object.display( 'pass' );
this.effects_object.fade( 'down', this.TIME );
return false;
}
break;
case 'tweet':
if( !this.text_object.checkPattern( 'tweet' ) )
{
this.message_object.display( 'tweet' );
this.effects_object.fade( 'down', this.TIME );
return false;
}
break;
default:
}
}
var response_element = this.response_element;
if( this.type === 'tweet' ) { var fill_element = this.fill_element; }
new Ajax().invoke( serializeArray( this.form_element ) + '&ajax_type=' + this.type + '_control', function( server_response_text ) { ajaxType( server_response_text, response_element, 'respond', fill_element ); } );
};
Control.tweet = function()
{
new Control( 'tweet' ).invoke();
}
Control.in = function()
{
new Control( 'signin' ).invoke();
};
Control.up = function()
{
new Control( 'signup' ).invoke();
};
Control.out = function()
{
new Ajax().invoke( '&ajax_type=ControlSignOut', function( server_response_text ) { ajaxType( server_response_text, 0, 'simple' ); } );
};
Control.try = function()
{
new Ajax().invoke( '&ajax_type=ControlTryIt', function( server_response_text ) { ajaxType( server_response_text, 0, 'simple' ); } );
};
Control.bookmarkDelete = function( event_pull )
{
event_pull.preventDefault();
domBookmarkDelete( this );
new Ajax().invoke( encodeURIComponent( this.name ) + "=" + encodeURIComponent( this.innerHTML ) + '&ajax_type=ControlBookmark_delete', function( ) { } );
}
return Control;
} () );