简体   繁体   中英

What are all the ways you can pass arguments into functions in JavaScript?

Coming from Python into some JavaScript-based APIs I'm confused by some of the syntax. And I can't find an answer in all of the noise of random information about declaring functions.

In Python, you can mix specifying arguments to a function base on the order and based on the name: np.arange(1,5,step = 5)

Can you do something like that in Javascript?

If there is a function like: ee.List.sequence(start, end, step, count ) and it only needs three out of the four arguments I can really easily specify the start, end, step, like so: ee.List.sequence(1,100,2)

But, do I have to use the object notation to specify the count? ee.List.sequence({start=1,end=100, count=50})

Is there a shorthand, like in Python, such as: ee.List.sequence(1,100,{count=50}) or ee.List.sequence(1,100,,50) ?

It seems that what you are really asking is less about JavaScript as a language and more about specific APIs. So, here's some things to know:

In JavaScript, all arguments are optional. In other words, there is no way to enforce that a function is called with the proper amount or order of arguments. It's up to the caller to know the signature of the function its calling and call it appropriately. It's also up to the creator of the function to be prepared for some or all of the arguments to not be passed. There is an arguments array-like object that all functions have that can assist with this, but checking the inputs is also pretty easy. Here's an example:

 // Here's an example of a function that does not explicitly declare any arguments function foo1(){ // However, arguments might still be passed and they can be accessed // through the arguments object: console.log("Arguments.length = ", arguments.length); console.log(arguments); } foo1("test", "boo!"); // Call the function and pass args even though it doesn't want any // *********************************************** // Here's an example of a function that needs the first arg to work, // but the seond one is optional function foo2(x, y){ if(y){ console.log(x + y); } else { console.log(x); } } foo2(3); foo2(4, 5);

In JavaScript, your functions can take any valid primitive or object. Again, it's up to the caller to know what the API is and call it correctly:

 function foo1(string1, number1, object1, string2){ console.log(arguments); } foo1("test", 3.14, {val:"John Doe"}, "ing");

I've found the answer in this JavaScript tutorial .

In general, yes, the default JavaScript function can take anything as an argument. But any well-written API function, with specified arguments (and default values), will not allow this to happen.

So the two options are

  1. Supply the arguments in order, without naming them.

     `ee.List.sequence(1,100,2)`
  2. Pass them in a named object (where suddenly order doesn't matter)

     `ee.List.sequence({start=1,end=100, count=50})`

There is no mixed notation like Python has ee.List.sequence(1,100,{count=50})

BUT there is a workaround for 1 In case you want to use a different combination of arguments, one can supply null values to the arguments that are omitted. So the following can be used:

`ee.List.sequence(1,100,null,50)`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM