DRY - 不要重复自己
ControlSignUp 和 ControSignIn 几乎相同。我在仅有的 4 行不同的地方评论了“这里”。我怎样才能结合这个常见的功能?
实际上这似乎很明显......我可以通过构造函数传入一个变量......只需一秒钟。
回答:
/**
* ControlSign
*/
var ControlSign = function( type )
{
var form_element = document.getElementById( type );
var response_element = document.getElementById( type + '_response' );
var text_object = new Text( form_element );
var message_object = new Message( response_element );
this.invoke = function( )
{
if( Global.validate_input_on === 1 )
{
if( !text_object.checkEmpty() )
{
message_object.display( 'empty' );
return false;
}
if( type === 'signup' && !text_object.checkPattern( 'name' ) )
{
message_object.display( 'name' );
return false;
}
if( !text_object.checkPattern( 'email' ) )
{
message_object.display( 'email' );
return false;
}
if( !text_object.checkPattern( 'pass' ) )
{
message_object.display( 'pass' );
return false;
}
}
AjaxNew.repeatUse( ajaxSerialize( form_element ) + '&ajax_type=' + type + '_control', function( server_response_text ) { ajaxType( server_response_text, response_element, 'respond' ); } );
}
};
ControlSign.in = function()
{
new ControlSignIn( 'signin' ).invoke();
};
ControlSign.up = function()
{
new ControlSignUp( 'signup' ).invoke();
};