I'm working on a dynamic validation system. For some reason, shift() on the args array mainfunc passes to validateNumber does not act properly. Here's the code with output in comments next to alert boxes:
function mainfunc (func){
//this calls the function validateNumber and passes args to it.
this[func].apply(this, Array.prototype.slice.call(arguments, 1));
}
function validateNumber(args) {
alert(args); //this functions normally. for example, displays fish,3,5
var text = args.shift; //would expect this to return 'fish', right?
alert(text); //instead of 'fish' alerts 'function shift() { [native code] }'. This is the problem.
var minimum = args.shift;
var maximum = args.shift;
return text;
}
validationArgs = classList[i].split('-');
functionName = validationArgs.shift();
validationArgs.unshift($(this).val());
mainfunc(functionName, validationArgs); //calls mainfunc which calls the function
I'm stumped as to why this behaves this way. Note: I cribbed mainfunc from this StackOverflow answer: Calling dynamic function with dynamic parameters in Javascript
Edit: Oh, my goodness. I am an idiot. I even use shift() correctly in the title of the question! Thanks all.