Js constructs a function with two variables inside. Can I just pass one in when I call it?

A move (first,modify) method is defined in

js, which needs to pass in first,modify. Because I want to reuse the move function, but there are places where you don"t need modify, and places where you don"t need it, even if you pass a null value, you can"t run it.

is there any way to run without passing a fixed number of parameters?


function moveTest() {
    var first = document.getElementById(id);
    var modify = {
        y: function () {
           statements
        }
    };
   move(first, modify); //
}

    function move(first, modify) { 
        first.onmousedown = function (e) {      //onclickonmouseover,onmousedown
            modify.y(); //
            var x = e.clientX - first.offsetLeft;
            var y = e.clientY - first.offsetTop;
            document.onmousemove = function (e) {
                var left = e.clientX - x;
                var top = e.clientY - y;
                first.style.left = left + "px";
                first.style.top = top + "px";

            document.onmouseup = function () {  //;
                document.onmousemove = null;
                document.onmouseup = null;
            }
        }
    },
Mar.03,2021

Thank you for the invitation

I see that this modify.y (); is called in your move . If not, an error will be reported. You can first determine whether mdify exists, and then execute the y method, so that an error will not be thrown.

if(mdify){
    modify.y(); //
}

how comfortable it is to use objects for parameters

function move (obj) {}

call:

obj = {
    first:'',
    modify:''
}
move(obj);

you can use ES6 syntax to pass a default value for function parameters, or refer to Bai Yinling's method to judge

in the function.
Menu