Code Library
Home Submit Free Hosting Link To Us Contacts

JavaScript Function to Verify Correct Number of Arguments

JavaScript Function to Verify Correct Number of Arguments JavaScript JavaScript Function to Verify Correct Number of Arguments Download (.zip)



function checkargs() {
    // arguments.caller.callee is the Function object that called us.
    // Its arity property is the number of arguments that were expected.
    var expected = arguments.caller.callee.arity;
    // arguments.caller is the arguments object of the function that
    // called us. Its length property is the number of actual args passed.
    var passed = arguments.caller.length;

    // If they don't match, do some fancy regular expression work to get
    // the name of the calling function, and display a warning.
    if (passed != expected) {
      var funcname = arguments.caller.callee.toString().match(/function (\w*)/)[1];
      alert("WARNING:\n" +
          funcname + "() " + "was invoked with wrong number of arguments!\n" +
          "Expected " + expected + " arguments, but passed " + passed);
    }
}

// Here is a test function that uses checkargs().
function f(x,y,z) { checkargs(); return x+y+z; }
f(1,2,3);        // Passed the right number of arguments
f(1,2);          // Passed too few arguments; checkargs() displays a warning.







Tatet