Handling optional parameters in javascript

前端 未结 12 927
无人共我
无人共我 2020-11-28 19:19

I have a static javascript function that can take 1, 2 or 3 parameters:

function getData(id, parameters, callback) //parameters (associative array) and callb         


        
12条回答
  •  半阙折子戏
    2020-11-28 19:52

    This I guess may be self explanatory example:

    function clickOn(elem /*bubble, cancelable*/) {
        var bubble =     (arguments.length > 1)  ? arguments[1] : true;
        var cancelable = (arguments.length == 3) ? arguments[2] : true;
    
        var cle = document.createEvent("MouseEvent");
        cle.initEvent("click", bubble, cancelable);
        elem.dispatchEvent(cle);
    }
    

提交回复
热议问题