1

Let's say I have this code:

function QueCalculamos(valor) {

    function CalculaADeB() {
      console.log( "Calculando A de B");
      }

    function CalculaBDeA() {
      console.log( "Calculando B de A");
      }


   if (valor =="A") { return CalculaADeB; }
    else if (valor == "B") { return CalculaBDeA; }
}

let bvla="B";

var exe = QueCalculamos(bvla);
exe();

Depending on the value I pass to "QueCalculamos", it will execute one function or the other.

But let's suppose that I don't have to choose between two functions, but among many, and that they are a bit large. For code readability purposes, I would like to define "CalculaADeB" and "CalculaBDeA" separately, outside "QueCalculamos".

How would I return a reference to those functions in that case? And if it's not possible, how would you organize the code to make it more readable then? (Maybe I don't have enough experience with modern Javascript, but this whole "define a function inside a function" thing always looks messy to me).

1
  • 1
    Define them wherever. Put them into an array. Pass the array as a variable. Commented Jul 14, 2020 at 10:07

3 Answers 3

3

You can define your functions as methods of an object.

let funcs = {
    func1() { /* ... */ },
    func2() { /* ... */ },
    /* ... */
}

Then reference these from your router function via something like a switch statement.

function QueCalculamos(valor) {
    switch (valor) {
        case 'A': return funcs.func1;
        case 'B': return funcs.func2;
        /* ... */
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

You can define them wherever you want, as long as they are in scope in QueCalculamos so that you can access (get a reference to) them. The only difference is that the functions will no longer be closures that could access the valor - but they don't do that anyway.

It will just work if you move them outside:

function CalculaADeB() {
  console.log("Calculando A de B");
}
function CalculaBDeA() {
  console.log("Calculando B de A");
}

function QueCalculamos(valor) {
  if (valor =="A") { return CalculaADeB; }
  else if (valor == "B") { return CalculaBDeA; }
}

let bvla="B";

var exe = QueCalculamos(bvla);
exe();

You might be able to further simplify the code by putting them on an object with property names to select them:

const calcs = {
  A() {
    console.log("Calculando A de B");
  },
  B() {
    console.log("Calculando B de A");
  },
};
function QueCalculamos(valor) {
  return calcs[valor];
}

var exe = QueCalculamos("B");
exe();

Comments

2

Group them into an object instead of making them separate functions, and access them using a key:

let functions = {                                          // keys are the possible values of valor
  "A": function() { /* ... */ },                           // you can either define the functions here
  "B": CalculaBDeA,                                        // ... or assign a reference to an already existing function
  "C": function() { /* ... */ },
  "D": function() { /* ... */ },
  /* ... */
}

function QueCalculamos(valor) {
  if(functions.hasOwnProperty(valor)) {                     // if the functions object contains a function for valor
    return functions[valor];                                // return it
  }

  throw "there is no function for '" + valor + "'";        // otherwise throw an error or return something else to signal failure
}

Demo:

let functions = {
  "A": function() {
    console.log("Function A");
  },
  "B": function() {
    console.log("Function B");
  },
  "C": function() {
    console.log("Function C");
  },
  "D": function() {
    console.log("Function D");
  },
  /* ... */
}

function QueCalculamos(valor) {
  if (functions.hasOwnProperty(valor)) {
    return functions[valor];
  }

  throw "there is no function for '" + valor + "'";
}


let exe = QueCalculamos("C");
exe();

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.