B
B
beginer1232017-07-21 01:15:02
JavaScript
beginer123, 2017-07-21 01:15:02

What is the best way to pass arguments to a JS function?

Hello, how do pros usually pass arguments to functions?
one way or better to do it this way You can not write the option as you want, I understand this myself
function(a,b,c,d,.....){}
function({arg1:a,arg2:b,arg3:d,..... }){}

Answer the question

In order to leave comments, you need to log in

5 answer(s)
P
profesor08, 2017-07-21
@beginer123

If the number of arguments is variable and they can change at all, then the object.

func({
 mama: "vanea",
 papa: "vasea",
 ujas: true
});

If the quantity is fixed, then as usual:
func(mama, papa, ololo, trololo);

G
gleendo, 2017-07-21
@evgeniy8705

class Super() {
  constructor({height = 300, width = 300} = {}) {
    this.width = width;
    this.height = height;
  }
}

new Super({
  width: 200
});

// -----------------------------------------

const func = function(a, b, c, ...args) {
  // some code
}

func(1, 2, 3, 4, 5, 6, 7, 8);

T
ThemeZV, 2017-07-21
@ThemeZ

Usually, if a function takes more than three three arguments, it's better to concatenate them into an object and pass the object

V
Vitaly, 2017-07-21
@vshvydky

const fName = ({param1, param2 ... paramN}) => ...

V
Vasily Nazarov, 2017-07-21
@vnaz

If there are few options (yes, few - it's " as you wish "), then you can use a list.
If a lot, and invariant - better structure.
Extended:
The function is almost always called as f(x) , rarely as f(x, y) , and almost never as f(a, b, c).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question