|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script>
 /**//**
* 驗(yàn)證傳入的參數(shù)(args)是否符合(types)類型,即驗(yàn)證一個方法的實(shí)參類型
* @param {Object} types 參數(shù)類型數(shù)組
* @param {Object} args 參數(shù)數(shù)組
*/
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>

|