I'm working on a function with many parameters, and would like to be able to name them explicitly when calling the function.

I would usually call a function like this:

function get_sum(value_1, value2) {
var Sum = ee.Number(value_1).add(ee.Number(value2)); 
// Working
get_sum(75, 82);

Naming the parameters when calling the function leads to an error:

//Produces error
get_sum({value_1: 75, value2: 82});

enter image description here


How do I do this? Is it something to do with served vs. client side? Link to code


This happens to be a matter of pure JavaScript; when you write a function and call it yourself, the Earth Engine API is not involved at all.

Here is how to write your function to accept {} named arguments:

function get_sum(args) {
    var value_1 = args.value_1;
    var value_2 = args.value_2;
    var Sum = ee.Number(value_1).add(ee.Number(value2)); 

(args is just a regular function parameter variable, not a special keyword, and you can call it whatever you like. args is a common name for this sort of thing.)

If you want to accept either named or un-named (positional) arguments, like the EE API functions do, it's significantly more messy and I'd recommend not bothering, but if you like, I can explain that too.

Note that if you misspell a name, the variables in the function will have the value undefined. If you want to help yourself out at the cost of writing more code, you can check for that:

function get_sum(named_args) {
    var value_1 = named_args.value_1;
    var value_2 = named_args.value_2;
    if (value_1 === undefined) throw new Error('get_sum: value_1 is missing');
    if (value_2 === undefined) throw new Error('get_sum: value_2 is missing');

    var Sum = ee.Number(value_1).add(ee.Number(value2)); 

Side note: Current JavaScript versions support a shortcut that makes this much more convenient:

function get_sum({value_1, value_2}) {
    var Sum = ee.Number(value_1).add(ee.Number(value2)); 

Unfortunately, the Earth Engine Code Editor does not currently permit running scripts that use newer JavaScript features. I mention this only because you may see references to this feature when reading about JavaScript.

  • Thanks for taking the time to answer this so extensively, very helpful stuff! – Joris Jul 23 '20 at 15:57
  • I have wondering how earth engine functions "accept either named or un-named (positional) arguments". It would be good if you can explain that. I just come across this answer and I am suddenly more curious. I can ask a new question if needed. – Liman Mar 11 at 22:27
  • 1
    @Liman Each function checks if whether it has received a single object argument or more than one argument. You can examine the implementation here in arguments.js which has some informative comments about the conditions under which this is possible. – Kevin Reid Mar 11 at 22:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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