code:
//依据参数列表来严格地检查一个变量列表的类型 function strict( types, args ) { //确保参数的数目和类型核匹配 if ( types.length != args.length ) { //如果长度不匹配,则抛出异常 throw "Invalid number of arguments. Expected " + types.length + ", received " + args.length + " instead."; } //遍历每一个参数,检查基类型 for ( var i = 0; i < args.length; i++ ) { //如 javascript 某一项类型不匹配,则抛出异常 if ( args[i].constructor != types[i] ) { throw "Invalid argument type. Expected " + types[i].name +", received " + args[i].constructor.name + " instead."; } } }
打印code不懂:
//用来打印出用户列表的一个简单函数 function userList( prefix, num, users ) { //确保 prefix 是一个字符串,num 是一个数字, //且 user 是一个数组 strict( [ String, Number, Array ], arguments ); //循环处理 num 个用户 for ( var i = 0; i < num; i++ ) { //显示一个用户的信息 print( prefix + ": " + users[i] );