// Strictly check a list of variable types against a list of arguments
function strict(types, args) {
// Make sure that the number of types and args matches
if (types.length != args.length) {
// If they do not, throw a useful exception
throw "Invalid number of arguments. Expected " + types.length +
", received " + args.length + " instead.";
}
// Go through each of the arguments and check their types
for (var i = 0; i < args.length; i++) {
//
if (args[i].constructor != types[i]) {
throw "Invalid argument type. Expected " + types[i].name +
", received " + args[i].constructor.name + " instead.";
}
}
}
// A simple function for printing out a list of users
function userList(prefix, num, users) {
// Make sure that the prefix is a string, num is a number,
// and users is an array
strict([String, Number, Array], arguments);
// Iterate up to 'num' users
for (var i = 0; i < num; i++) {
// Displaying a message about each user
alert(prefix + ": " + users[i]);
}
}
var func = function func() {};
var users = ['one','two','three'];
//userList('USERS', func, users); // NEVER GETS HERE DUE TO EXCEPTION BEING THROWN
userList('USERS', users.length, users);
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.