Pass unknown number of arguments into javascript function

后端 未结 12 1289
情话喂你
情话喂你 2020-12-02 10:17

Is there a way to pass an unknown number of arguments like:

var print_names = function(names) {
    foreach(name in names) console.log(name); // something li         


        
12条回答
  •  醉梦人生
    2020-12-02 10:47

    You can create a function using the spread/rest operator and from there on, you achieved your goal. Please take a look at the chunk below.

    const print_names = (...args) => args.forEach(x => console.log(x));
    

提交回复
热议问题