Question on Javascript Function Parameters

Viewed 15966

I was trying to write some javascript function and realised that

function testFunction(input_1, input_2, input_3) {
       alert("alert");
}

however when i call the function like this:

<input type="button" value="click" onclick="testFunction("1", "2")">

why will it still work even with just two parameters?

8 Answers

Javascript does not support Method overloading hence method will be execute in order of their occurence irrelevant of arguments passed Because javascript has no type checking on arguments or required qty of arguments, you can just have one implementation of testFunction() that can adapt to what arguments were passed to it by checking the type, presence or quantity of arguments..

Try using the below code

var CVH = {
        createFunction: function (validationFunction, extParamData, extParamData1) {
            var originalFunction = validationFunction;
            var extParam = extParamData;
            var extParam1 = extParamData1;

            return function (src, args) {
                // Proxy the call...
                return originalFunction(src, args, extParam, extParam1);
            }
        }
    }


function testFunction(input_1, input_2, input_3) {
       alert("alert");
}

and you can call this function as below

<input type="button" value="click" onclick="CVH.createFunction(testFunction('1', '2'),'3','4')">
Related