Answer the question
In order to leave comments, you need to log in
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 myselffunction(a,b,c,d,.....){}
function({arg1:a,arg2:b,arg3:d,..... }){}
Answer the question
In order to leave comments, you need to log in
If the number of arguments is variable and they can change at all, then the object.
func({
mama: "vanea",
papa: "vasea",
ujas: true
});
func(mama, papa, ololo, trololo);
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);
Usually, if a function takes more than three three arguments, it's better to concatenate them into an object and pass the object
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 questionAsk a Question
731 491 924 answers to any question