[removed] Configuration Pattern

前端 未结 3 1268
情话喂你
情话喂你 2020-12-18 07:07

Problem: A Javascript function needs few parameters to work with:

function kick(person, reason, amount) {
    // kick the *person* with the         


        
3条回答
  •  太阳男子
    2020-12-18 07:48

    JavaScript functions are signed by their name only.

    Hence you can do:

      function kick(x, reason, amount) {
          if(reason && amount) {
              // do stuff with x as person, reason and amount
          }
          else if(x) {
              // do stuff with x as config
          }
          else {
             // do stuff with no parameters
          }
    
        }
    

    another solutions is to use the arguments variable which is an array that holds all parameters that passed to a function in javascript

       function kick() {
                alert(arguments.length);
       }
    

提交回复
热议问题