gpt4 book ai didi

javascript - 代码整合与复杂性

转载 作者:行者123 更新时间:2023-11-29 18:29:01 25 4
gpt4 key购买 nike

<分区>

我有一些处理用户操作的模块——它们是控制模块。有 6 个,现在有 5 个,因为我合并了 2 个非常相似的模块,称为控制

我注意到,当我合并类似的代码时......它变得有点低效。例如,在 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;
} () );

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com