|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script>
/**//**
* 验证传入的参数(args)是否符合(types)类型,即验证一个方法的实参类型
* @param {Object} types 参数类型数组
* @param {Object} args 参数数组
*/
function strict(types, args)
{
if (types.length != args.length) {
throw "Invalid number of arguments. Excepted " + types.length + ",received " + args.length + " instead.";
}
for (var i = 0; i < args.length; i++) {
if (args[i].constructor != types[i]) {
throw "Invalid argument type. Excepted " + types[i].name + ",received " + args[i].constructor.name + " instead.";
}
}
}
function userList(prefix, num, users){
strict([String, Number, Array], arguments);
for (var i = 0; i < num; i++) {
document.write(prefix + ":" + users[i]);
document.write("<br />");
}
}
var users = ["zdw", "admin", "aoyun"];
userList("hello : ", users.length, users);
</script>
</head>
<body>
</body>
</html>
|