title: Using object to choose which function to run (dynamically)
body: <p>From client side (html), form is sent and server side decided which function to run by the one of the select option.</p>
  
  <pre><code>const decideWho = (form) =&gt; {
    const choice = form.choice;
    const obj = { key1 : 'func1(form.input1)',
  key2 : 'func2(form.input2)'......};
  return eval(obj[choice]||obj[default]);
  }
  </code></pre>
  
  <p>Due to some other question, this code was put and <em>Josh Wulf</em> mentioned the danger of using eval.</p>
  
  <p>So here I'm. I couldn't search related to my situation so if you know any answered question, let me know.</p>
  
  <p>My problem is, at first I just put obj's value without quotation mark. Then the disaster happened. When the last line <strong>obj[choice]||obj[default]</strong> is called, all the functions in the obj are called and some functions were iterating repeatedly. I didn't know what went wrong.</p>
  
  <p>Not only this.</p>
  
  <pre><code>const someFunc = () =&gt; {
  const a = {
        sourceFolder: "",
        aFolder: "1OoK3j",
        bFolder: "1M_cyv",
        cFolder: "11maBJ",
        dFolder: "1QxA8P",
        eFolder: "11lG"};
      for (let i in a) 
        eval(`var ${i} = getFolder(a['${i}']);`);
  
  ..move files to the destination above.
  }
  
  
  const getFolder = id =&gt; {
    try {
      if (id) {
        return DriveApp.getFolderById(id);
      } else return DriveApp.getRootFolder();
    } catch (e) {
    // If the folder by the id doesn't exists, return root folder.
      return DriveApp.getRootFolder();
    }
  }
  
  </code></pre>
  
  <p>I didn't want to declare each folder using same function. So I put them in obj and without eval, iterated.</p>
  
  <pre><code>for (let i in a) var i = getFolder(a[i]);
  </code></pre>
  
  <p>Here again disaster. When I called aFolder expecting it would return the folder with the id mentioned, it iterates all the folders in the object.</p>
  
  <p>Therefore, for the rescue, values are wrapped around with quotation mark and eval temporally.</p>
  
  <p>What is supposed to be done here?</p>
  
  <p>** Edited **
  I'm trying to adopt function constructor.</p>
  
  <pre><code>const aa = form =&gt; `id: ${form.id}`;
  const bb = () =&gt; "I'm b";
  const aaa = form =&gt; {
    const obj = {
      key1 : 'aa(form)',
      key2 : 'bb'
    };
    const handleIt = new Function('return ' + obj[form.choice])();
    console.log(handleIt());
  }
  const cc = () =&gt; {
    let form = {id: 'student 1', choice: 'key1'};
    aaa(form);
    form = {id: 'student 2', choice: 'key2'};
    aaa(form); 
  }
  </code></pre>
  
  <p><em>These are simplified only to ask a question but real functions are long.</em>
  When I perform</p>
  
  <pre><code>form = {id: 'student 2', choice: 'key2'};
    aaa(form); 
  </code></pre>
  
  <p>It works as expected. But when an argument is supposed to be passed, it says form is not defined.</p>
  
  <pre><code>let form = {id: 'student 1', choice: 'key1'};
    aaa(form);
  </code></pre>
  
  <p>Where and how do I need to add the arguments if function constructor is the solution?</p>
  
  <pre><code>const aaa = form =&gt; {
    const obj = {
      key1 : 'aa(form)',
      key2 : 'bb'
    };
    const handleIt = new Function('return ' + obj[form.choice])();
    console.log(handleIt());
  }
  </code></pre>
  
  <p><strong>Edited 2</strong> I got first one done without eval. </p>
  
  <pre><code>const decideWho = myForm =&gt; {
    const obj = {
      key1 : func1(myForm),
      key2 : func2()
    };
    const handleIt = () =&gt; {return obj[myForm.choice];};
    console.log(handleIt());
  }
  </code></pre>