3

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?

thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
dush
  • 211
  • 4
  • 15
  • 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 Nov 02 '13 at 19:08
  • why not u are calling one function recursively? – thecodeparadox Nov 02 '13 at 19:09
  • possible duplicate of [Javascript - Variable in function name, possible?](http://stackoverflow.com/questions/3733580/javascript-variable-in-function-name-possible) – Joren Nov 02 '13 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 complicated 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 Nov 02 '13 at 19:13
  • 1
    Why dont you move the screen id into the parameter, instead of function name. myScreen1() would become myScreen(screenId) { //do common things here //plus view specific thins below if (screenId == 1) {screen 1 specific things go here etc} } – Rainer Plumer Nov 02 '13 at 19:18

4 Answers4

9

Following 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;
gouessej
  • 3,640
  • 3
  • 33
  • 67
Peter van der Wal
  • 11,141
  • 2
  • 21
  • 29
3

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?

Oriol
  • 274,082
  • 63
  • 437
  • 513
2

wrap them in another function.

function handleScreen( screenName ) {
    // do stuff in common 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");
Rainer Plumer
  • 3,693
  • 2
  • 24
  • 42
0

The following should work, but could be compartmentalized 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 compartmentalize 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.