I have a requirement like, i need to execute a js code for some screens in my application and i need to name the function with viewid like myScreen1().. myScreen3().
But as the function definition is same for all the function names, is there any way to declare such that all the function names will refer to same definition?
I have a requirement like, i need to execute a js code for some screens in my application and i need to name the function with viewid like myScreen1().. myScreen3().
But as the function definition is same for all the function names, is there any way to declare such that all the function names will refer to same definition?
Share Improve this question edited Nov 2, 2013 at 19:08 thecodeparadox 87.1k22 gold badges141 silver badges164 bronze badges asked Nov 2, 2013 at 19:08 dushdush 2095 silver badges16 bronze badges 5- 2 Why not just use the same function repeatedly? Function names should be related to their purpose, not the context from which they are called. – Rory McCrossan Commented Nov 2, 2013 at 19:08
- why not u are calling one function recursively? – thecodeparadox Commented Nov 2, 2013 at 19:09
- possible duplicate of Javascript - Variable in function name, possible? – Joren Commented Nov 2, 2013 at 19:10
- i can use the same function repeatedly, but i have more than 10 screens, and the code will be repeated.. And calling the same function for these screens is little plicated in my application and for future maintenance, if i want to call this for some more screens, i can call this dietly by making a simple change in my js. Is there any way to define like same definition with muliple names with out code repitition? – dush Commented Nov 2, 2013 at 19:13
- 1 Why dont you move the screen id into the parameter, instead of function name. myScreen1() would bee myScreen(screenId) { //do mon things here //plus view specific thins below if (screenId == 1) {screen 1 specific things go here etc} } – Rainer Plumer Commented Nov 2, 2013 at 19:18
4 Answers
Reset to default 9Following will work, but maybe you should rethink your design as most likely it is also possible using only one function name.
var func = function() {
// definition goes here
}
var myScreen1 = func;
var myScreen2 = func;
Compacting a bit @Peter Van der Wal's code,
var myScreen1, myScreen2, myScreen3;
myScreen1 = myScreen2 = myScreen3 = function() {
// definition goes here
};
But really, if ...
function definition is same for all the function names
... couldn't you use myScreen()
instead of myScreen1()
, myScreen2()
or myScreen3()
?
And I'm curious: how do you decide which one you call, if all of them do the same?
wrap them in another function.
function handleScreen( screenName ) {
// do stuff in mon with all screens here
// and call specific functions for specific screens
if (typeof window[screenName] == "function") {window[screenName]();}
}
function screen1() {
//do stuff for screen 1
}
function screen2() {
//do stuff for screen 2
}
handleScreen("screen2");
The following should work, but could be partmentalized like shown at the end.
function handleScreen( viewID ) {
if(viewID == 1){
// Do Stuff For Screen 1
} else if(viewID == 2){
// Do Stuff For Screen 2
}
}
handleScreen(2);
This way you only have one function defined and if you want you could partmentalize by using certain functions like shown below.
function handleScreen( viewID ) {
if(viewID == 1){
displayLoadSign();
loadList();
hideLoadSign();
} else if(viewID == 2){
showConnectionError();
displayLoadSign();
}
}
handleScreen(2);
That way you don't have code written multiple times.