/    Sign up×
Community /Pin to ProfileBookmark

How to optimaze my code

Hello.

I have javvascript code on my site

[CODE]if (result[0] == “doCheckEmailAccountSettings”) {
emailAccoutSettingChecked(result);
return;
}
if (result[0] == “doEditEmailAccount”) {
showEditEmailAccount(result);
return;
}
if (result[0] == “doDeleteEmailAccount”) {
getAllForUser();
return;
}
if (result[0] == “doBackupEmailBox”) {
showBackupAccount(result);
return;
}
if (result[0] == “getAmountOfEmails”) {
doBackupInit(result);
return;
}[/CODE]

This listing is very long. Near 50 such functions.
Is it possible to optimise this code?

I can rename my function to make them simething like

[CODE]var functionname=’BackFunc_’+result[0];[/CODE]

How then i can call function and how ot check if function exists?

Thanks.

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@KorJul 14.2009 — 

I can rename my function to make them simething like
[CODE]var functionname='BackFunc_'+result[0];[/CODE]

How then i can call function and how ot check if function exists?

Thanks.[/QUOTE]


if so, simply:
<i>
</i>window['BackFunc_'+result[0]](result);
Copy linkTweet thisAlerts:
@gelembjukauthorJul 14.2009 — if so, simply:
<i>
</i>window['BackFunc_'+result[0]](result);
[/QUOTE]


Thanks.

But how can i check if function exists?
Copy linkTweet thisAlerts:
@KorJul 14.2009 — <i>
</i>if(window['BackFunc_'+result[0]]){
window['BackFunc_'+result[0]](result);
}
Copy linkTweet thisAlerts:
@Declan1991Jul 14.2009 — Or, simply a switch statement.<i>
</i>switch (result[0]) {
case 'whatever':stuff(); return;
// etc.
}
That's more flexible anyway.

I think that you have another problem though, how are you ending up in this situation?
Copy linkTweet thisAlerts:
@Sterling_IsfineJul 14.2009 — [CODE]
var actions = [], funcObj;

actions[ "doCheckEmailAccountSettings" ] = { f : emailAccountSettingChecked, p1 : result };
actions[ "doEditEmailAccount" ] = { f : showEditEmailAccount, p1 : null };
actions[ "doDeleteEmailAccount" ] = { f : getAllForUser, p1 : null };
actions[ "doBackupEmailBox" ] = { f : showBackupAccount, p1 : result };
actions[ "getAmountOfEmails" ] = { f : doBackupInit, p1 : result };

if( ( funcObj = actions[ result[0] ] ) != undefined )
funcObj.f( funcObj.p1 );[/CODE]
Copy linkTweet thisAlerts:
@gelembjukauthorJul 15.2009 — <i>
</i>if(window['BackFunc_'+result[0]]){
window['BackFunc_'+result[0]](result);
}
[/QUOTE]


Thanks. This code works.

Currently i tested only in FireFox.
Copy linkTweet thisAlerts:
@KorJul 15.2009 — Thanks. This code works.

Currently i tested [COLOR="Blue"]only in FireFox[/COLOR].[/QUOTE]

It does not matter. That is basic javascript syntax, it should work in all the browsers, even in some dead-and-buried ones, like IE4 or NS4... ?

Basically, all the functions or global variables are nothing but [I]properties[/I] of the Global Object which, in javascript, is the [B]window[/B] element.
×

Success!

Help @gelembjuk spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...