0

I want to setup a function to return global variables and objects and then use eval on them on the calling function but I'm struggling to work out how to do this.

I realise I need to return them as a string and have tried a few things. Now at this:

//global vars etc for functions
function globals(name){
  
  let returnVal;
  
  switch (name){
    case "scEmailFindReplace":
      returnVal = [
        ["MULTI_LINE_ADDRESS_NAME", contact.sendName],
        ["MULTI_LINE_ADDRESS_FLAT", "Flat "+contact.flatNum+" Glenmore"],
        ["INV_NO", invNum],
        ["INV_DATE", scVars.INV__DATE],
        ["DEM_DATE", scVars.DEM__DATE],
        ["INV_START", scVars.INV_START],
        ["INV_END", scVars.INV_END],
        ["PAY_REF", "Glen "+contact.flatNum],
        ["SC_NET", scVars.DEM_AMOUNT],
        ["SC_GROSS", scVars.DEM_AMOUNT],
        ["TOT_NET", scVars.DEM_AMOUNT],
        ["TOT_GROSS", scVars.DEM_AMOUNT]
      ].toString();
      break;
    default:
      returnVal = "NOT FOUND";
      break;
  }
  
  return returnVal
}

But no luck. Still tries to evaluate it before the return.

Chris Barrett
  • 571
  • 4
  • 23
  • 1
    Off the bat, `eval` is a bad idea. Whatever you're attempting to do, step back and think why this is needed. May be there's a better way(I'm sure there is) – TheMaster Sep 25 '20 at 16:19
  • hmm. i suspect you're probably right. I'll have a head scratch. Why so anti `eval` out of interest? Cheers – Chris Barrett Sep 25 '20 at 16:48
  • @ChrisBarrett - because `eval` is a huge security risk with untrusted input. And it is a performance hit on trusted input. And because we have better alternatives like `JSON` built-in object. And because global variables are a bad idea in general ( "why" can make for a book chapter in itself ). Now, why would you want to do what you asked about? – Oleg Valter is with Ukraine Sep 26 '20 at 15:14
  • 1
    @ChrisBarrett, alternatively, you can read this: https://stackoverflow.com/questions/86513/why-is-using-the-javascript-eval-function-a-bad-idea – Oleg Valter is with Ukraine Sep 26 '20 at 15:21

1 Answers1

0

You can use JSON.stringify to get string that can be eval'ed later

const contact = {},
  scVars = {},
  invNum = "";

//global vars etc for functions
function globals(name) {

  let returnVal;

  switch (name) {
    case "scEmailFindReplace":
      returnVal = JSON.stringify([
        ["MULTI_LINE_ADDRESS_NAME", contact.sendName],
        ["MULTI_LINE_ADDRESS_FLAT", "Flat " + contact.flatNum + " Glenmore"],
        ["INV_NO", invNum],
        ["INV_DATE", scVars.INV__DATE],
        ["DEM_DATE", scVars.DEM__DATE],
        ["INV_START", scVars.INV_START],
        ["INV_END", scVars.INV_END],
        ["PAY_REF", "Glen " + contact.flatNum],
        ["SC_NET", scVars.DEM_AMOUNT],
        ["SC_GROSS", scVars.DEM_AMOUNT],
        ["TOT_NET", scVars.DEM_AMOUNT],
        ["TOT_GROSS", scVars.DEM_AMOUNT]
      ]);
      break;
    default:
      returnVal = "NOT FOUND";
      break;
  }

  return returnVal
}

console.log(globals("scEmailFindReplace"));
ponury-kostek
  • 7,824
  • 4
  • 23
  • 31