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?

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. Commented Nov 2, 2013 at 19:08
  • why not u are calling one function recursively? Commented Nov 2, 2013 at 19:09
  • possible duplicate of Javascript - Variable in function name, possible? 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 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? Commented Nov 2, 2013 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} } Commented Nov 2, 2013 at 19:18

4 Answers 4

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;
Sign up to request clarification or add additional context in comments.

Comments

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?

1 Comment

any ES6 way to achieve the same?
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");

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.