Click here to Skip to main content
15,915,509 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: I hate JavaScript Pin
Plamen Dragiyski30-Aug-16 0:20
professionalPlamen Dragiyski30-Aug-16 0:20 
GeneralRe: I hate JavaScript Pin
kalberts30-Aug-16 2:19
kalberts30-Aug-16 2:19 
GeneralRe: I hate JavaScript Pin
RugbyLeague30-Aug-16 2:42
RugbyLeague30-Aug-16 2:42 
GeneralRe: I hate JavaScript Pin
CygnusBMT30-Aug-16 3:24
CygnusBMT30-Aug-16 3:24 
GeneralRe: I hate JavaScript Pin
lopatir30-Aug-16 4:43
lopatir30-Aug-16 4:43 
GeneralRe: I hate JavaScript Also Pin
Steve Naidamast30-Aug-16 3:59
professionalSteve Naidamast30-Aug-16 3:59 
GeneralRe: I hate JavaScript Pin
rhyous30-Aug-16 4:51
rhyous30-Aug-16 4:51 
GeneralRe: I hate JavaScript Pin
Ryan Peden30-Aug-16 6:00
professionalRyan Peden30-Aug-16 6:00 
Although it won't make you hate JavaScript any less, you could use a countify factory function to make your existing functions pedantic about the number of arguments they receive:

JavaScript
// adapted from args length code found on SO
function argsLength(func) {  
    return (func + '')
      .replace(/[/][/].*$/mg,'') // strip single-line comments
      .replace(/\s+/g, '') // strip white space
      .replace(/[/][*][^/(*)]*[*][/]/g, '') // strip multi-line comments  
      .split('){', 1)[0].replace(/^[^(]*[(]/, '') // extract the parameters  
      .replace(/=[^,]+/g, '') // strip any ES6 defaults  
	  .replace(/[\(\)]/g, '') //strip any remaining brackets
      .split(',').filter(Boolean) // split & filter [""]
	  .length; //get length
}  

// made up all by myself
function countify(fn) {
	var expectedArgsCount = argsLength(fn);
	
	return function() {
		var receivedArgsCount = arguments.length;
		var expectedDescription = expectedArgsCount === 1 ? "argument" : "arguments";
		var receivedDescription = receivedArgsCount === 1 ? "argument" : "arguments";
		
		if(arguments.length !== expectedArgsCount) {	
			var errorMessage = ["Expected", 
								expectedArgsCount.toString(),
								expectedDescription + ",",
								"received", 
								receivedArgsCount.toString(), 
								receivedDescription + "."];
								
			throw new Error(errorMessage.join(" "));
		} else {
			fn.apply(this, arguments);
		}
	}
}


Then, with initializePage, you could change its definition to

JavaScript
var initializePage = countify(function(raceList, hlsList) {
    //your code here
});


but, since changing the function definitions of functions you want to countify is a pain, you could just do

JavaScript
initializePage = countify(initializePage)


anywhere after initializePage is defined. As a bonus, you can use this approach to countify library code as well, as long as you don't countify a function that does different things depending on the number of arguments it receives.

Heck, with a few small modifications, you could make countify check argument types as well, by making it take two arguments: an array containing the types of the function arguments, and then the function itself. Adding the checks will of course add a small amount of overhead at run time, but it won't be noticeable at all unless your function is being called tens of thousands of times in a tight loop...and maybe not even then, aside from the tens of thousands of error messages you'd see if you have the browser dev tools open.

As you mentioned, a lot of people will say "just use TypeScript!" which isn't a bad suggestion, but it's not always an option when you've got an existing code base and limited time.
GeneralI have found John's front yard Pin
Rage29-Aug-16 7:00
professionalRage29-Aug-16 7:00 
GeneralRe: I have found John's front yard Pin
Johnny J.29-Aug-16 7:09
professionalJohnny J.29-Aug-16 7:09 
GeneralRe: I have found John's front yard Pin
phil.o29-Aug-16 7:35
professionalphil.o29-Aug-16 7:35 
GeneralRe: I have found John's front yard Pin
Ravi Bhavnani29-Aug-16 8:12
professionalRavi Bhavnani29-Aug-16 8:12 
GeneralRe: I have found John's front yard Pin
#realJSOP29-Aug-16 8:40
professional#realJSOP29-Aug-16 8:40 
GeneralRe: I have found John's front yard Pin
Rage29-Aug-16 9:58
professionalRage29-Aug-16 9:58 
GeneralRe: I have found John's front yard Pin
#realJSOP29-Aug-16 11:13
professional#realJSOP29-Aug-16 11:13 
GeneralRe: I have found John's front yard Pin
Roger Wright29-Aug-16 20:05
professionalRoger Wright29-Aug-16 20:05 
GeneralRe: I have found John's front yard Pin
Daniel Pfeffer29-Aug-16 20:13
professionalDaniel Pfeffer29-Aug-16 20:13 
GeneralRe: I have found John's front yard Pin
dandy7229-Aug-16 8:45
dandy7229-Aug-16 8:45 
GeneralRe: I have found John's front yard Pin
Dr.Walt Fair, PE1-Apr-19 12:42
professionalDr.Walt Fair, PE1-Apr-19 12:42 
GeneralRe: I have found John's front yard Pin
Rage1-Apr-19 20:46
professionalRage1-Apr-19 20:46 
RantStackOverflow sucks Pin
Clifford Nelson29-Aug-16 6:58
Clifford Nelson29-Aug-16 6:58 
GeneralRe: StackOverflow sucks Pin
harold aptroot29-Aug-16 7:10
harold aptroot29-Aug-16 7:10 
GeneralRe: StackOverflow sucks Pin
Mark_Wallace29-Aug-16 7:21
Mark_Wallace29-Aug-16 7:21 
GeneralRe: StackOverflow sucks Pin
harold aptroot29-Aug-16 7:24
harold aptroot29-Aug-16 7:24 
JokeRe: StackOverflow sucks Pin
Johnny J.29-Aug-16 7:10
professionalJohnny J.29-Aug-16 7:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.