I have a module where I process some raster images according to one of different functions/ statistical models. I am trying to find a way to allow the user to select which function to use. I have tried storing the functions as variables or properties but this doesn't work. So my question here is how can I call a function using its name (string format)? Any other workaround to dynamically choose which function to launch?

Here is the link to a simplified example.

Here is the code for the example:

//first possible function to use
var sumFun = function(a)
  return ee.Number(a).add(3)

// second possible function to use
var subtFun = function(a)
  return ee.Number(a).subtract(2)

//Storing functions as properties (DOESN'T WORK)
var modDic = ee.Dictionary({'sumFun':sumFun, 'subtFun': subtFun})
var fun=modDic.get('sumFun')//calling function by its name
print(fun(4)) //doesn't work either atm

EDIT: corrected cast as dictionary

  • two things: 1. In your dictionary construction (which you cast as a feature instead of a dict), your "keys" are actual variables rather than strings. 2. I don't know if you can do what you want in GEE JS editor (certainly you can with the Python API), but you could just simply use some if statements.
    – Jon
    Commented Sep 12, 2019 at 14:03
  • Yes the cast as feature property was one of the tries. About the "if statement " solution, I will end up having 3 or 4 different models, and I would really like to avoid a sequence of "if "each time
    – ciskoh
    Commented Sep 13, 2019 at 9:03
  • 1
    You would only need a single function containing if statements to select the proper function. Pass it the keyword to select the proper function (as well as the function arguments). It's not elegant but in my experience with GEE, go with what works.
    – Jon
    Commented Sep 13, 2019 at 15:42

1 Answer 1


You might get by with something like:

var funcName = 'Number.add'

print(ee.apply(funcName, {
  left: ee.Number(1),
  right: ee.Number(2)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.