Allowing javascript function to accept any number of arguments

后端 未结 6 1799
难免孤独
难免孤独 2020-12-29 05:28

I would like the below function to be more flexible and accept multiple callbacks to other functions if they are defined in the arguments.

$(function() {
            


        
6条回答
  •  攒了一身酷
    2020-12-29 05:59

    Thanks to the spread operator now you can do something like this

    function myFunction(...params) {
      console.log(...params);
    }
    
    myFunction('Many arguments', 1, {two: 3}, ["four", 5]);
    
    // to access one by one
    function oneByOne(...params) {
      params.map(param => {
       console.log(param);   
      });
    }
    
    oneByOne('Many arguments', 1, {two: 3}, ["four", 5]);

提交回复
热议问题