1

Okay so in PHP we can call function as dynamic variables something like

$obj = new $class;
$obj->$myFoo($args);

But I am trying the same thing in Javascript but it's not working. The error is

Uncaught TypeError: foo is not a function 

My javascript code looks like this

function getId(arr){
    for(let i in arr){
        console.log("Function getId "+ arr[i]);
    }
}
function getMarker(arr){
    for(let i in arr){
        console.log("Function getMarker "+ arr[i]);
    }
}
const PAGEAPP = {
    processes:["getId","getMarker"],
    init:() => {
        for(let foo of PAGEAPP.processes){
            foo([1,2,3]);
        }
    },
}

Is there anyway I can access the same in Javascript.

Thank you in advance for your TIME :)

EDIT

This is what chrome showing

BlackXero
  • 880
  • 4
  • 17

1 Answers1

3

Use function names rather than using strings

function getId(arr) {
  for (let i in arr) {
    console.log("Function getId " + arr[i]);
  }
}

function getMarker(arr) {
  for (let i in arr) {
    console.log("Function getMarker " + arr[i]);
  }
}
const PAGEAPP = {
  processes: [getId, getMarker],
  init: () => {
    for (let foo of PAGEAPP.processes) {
      foo([1, 2, 3]);

    }
  },
}

PAGEAPP.init()
kooskoos
  • 4,622
  • 1
  • 12
  • 29
  • I know how to call it but My concern is after calling it, it's not working as supposed – BlackXero Jan 08 '20 at 07:24
  • It is doing what you are telling it to do. Run the code snippet. Also can you show us the expected output of your code? – kooskoos Jan 08 '20 at 07:26