例如:
let arr = [11, 22, 33, 44, 55, 66, 77, 88, 99]
function liFor(arr, callback) {
for (let i = 0i <arr.lengthi++) {
if (callback(arr[i])) {
console.log(arr[i])
}
}
}
console.log('---------遍历数组----------')
liFor(arr, val =>true)
console.log('---------输出奇数----------')
liFor(arr, val =>val % 2 !== 0)
console.log('---------输出被3整除----------')
liFor(arr, val =>val % 3 === 0)
console.log('---------输出被3和6整除----------')
liFor(arr, val =>val % 3 === 0 &&val % 6 === 0)
闭包函数概念:定义一个a方法,在a方法中定义一个b方法,并且b方法里面用到了a方法里面定义的变量,那么此时就形成了闭包函数,由于内部方法里面,用到外部方法里面的变量,外部方法里面的那个变量会一直在内存中存保存着。两个方法嵌套定义,里面的方法,用到了外面方法里面定义的变量,此时这两个方法就形成了闭包。
例如:
function a() {
console.log('a函数被调用了...')
let num1 = 100
let num2 = 200
function b() {
console.log('b函数被调用了...')
console.log(num1 + num2)
}
//返回的返回值也是一个函数,那么a函数就是高阶函数。
return b
}
// 通常情况下,函数执行完成后,函数里面定义的变量,会被销毁。
// a函数,已经调用完毕了,但是a函数里面定义变量,始终在内存中,因为b函数中用到了a函数中定义的变量。
// 那么此时这两个函数,就称之为:闭包函数。
let c = a()
c()
console.log('------------------------------------------')
// 闭包函数的实际案例
function calc(num1, num2, type) {
switch (type) {
case '+':
console.log(`${num1}+${num2}=${num1 + num2}`)
break
case '-':
console.log(`${num1}-${num2}=${num1 - num2}`)
break
}
}
// 在实际开发中,我们在做num1和num2的计算之前,可能需要先做其他事情
let num1 = 100
let num2 = 50
// 在做其他事情的过程中,我们的数据很有可能会被篡改。
console.log('查看用户是否登录')
num1 = 555
num2 = 145
console.log('检查用户的权限')
calc(num1, num2, '+') //运行结果不对,因为变量的值被篡改了。
console.log('------------------------------------------')
// 定义一个闭包函数,实现计算器功能
function myCalc(num1, num2, type) {
switch (type) {
case '+':
return function() {
return num1 + num2
}
case '-':
return function() {
return num1 - num2
}
}
}
//先准备好你的数据
let n1 = 100
let n2 = 50
//将你的数据传给计算器方法,由计算器方法,返回一个计算方法。
let js = myCalc(n1, n2, '+')
//在做具体的计算之前,还先做些其他的事情
console.log('查看用户是否登录')
n1 = 555
n2 = 145
console.log('检查用户的权限')
//其他事件准备好了后,执行计算方法
console.log(js())
把下面的这个做成一个js文件 然后就可以在任何页面调用了 /*================Method=======================* var_to_obj 将一个变量转换为对象* is_greater 判断是否大于某个数* is_less 判断是否小于某个数* Compare_Date 比较两个日期的大小 Num >Num return:trueNum <=Num return:false* is_numeric 判断是否是数字* is_price 判断是否是价格格式 带两位小数* Is_Null 判断是否是空true:空false:非空* IsSpace 判断是否空格* IsSpace 判断是否空格* Is_Int( 判断是否是整数true:是整数 false:不是整数* is_date 判断是否是日期* is_date 判断是否是日期* IsSelected 判断是否有有效数据被选中* isCharacter 检查是否是字符* isOtherNameCharacter 检查是否是其它可以作名称的字符* isNameCharacter 检查是否是可以作名称的字符* isEmail 检查是否是Email* isZIP 检查是否是邮编* isDigitalString 检查是否是数字字符串* IsEmpty IsEmpty函数判断一个字符串是否为空* Trim Trim函数去掉一字符串两边的空格* isDigital IsDigital函数判断一个字符串是否由数字(int or long)组成 * IsFloat IsFloat函数判断一个字符串是否由数字(int or long or float)组成 * IsTelephone 判断一个字符串是否由数字或 * () 组成 * dateTransfer 日期格式转化 / / * transferDate 转化日期 >/ / * ispassword 检查是否是密码* isLeapYear 判断是否为润年的函数* getDaysInMonth 取得每月天数的函数* jtrimstr 去掉字符串所有空格*=============================================*/
function doInputConnotEmpty(txtInput minLength sAlert){txtInput value=Trim(txtInput value)var m_IsValid=trueif(IsEmpty(txtInput value)) m_IsValid=falseif(minLength>){if(txtInput value length<minLength) m_IsValid=false}if(!m_IsValid){if(sAlert!= ) window alert(sAlert)txtInput focus()}return m_IsValid}
// 将一个变量转换为对象function var_to_obj(val){this value=val}// 判断是否大于某个数function is_greater(field crit limit){var Ret = (is_numeric(field ) ) ? (field value >limit ) : falseif (!Ret)doCritCode(field crit Value must be greater than +limit)return(Ret)}// 判断是否小于某个数function is_less(field crit limit){var Ret = (is_numeric(field ) ) ? (field value <limit ) : falseif (!Ret)doCritCode(field crit Value must be less than +limit)return(Ret)}
function is_numeric(field crit msg){var Ret = truevar NumStr= var decUsed=falsevar chrfor (i= i<field value length++i){chr=field value charAt(i)if (NumStr indexOf(chr )== ){if ( (!decUsed) &&chr== ){decUsed=true}else{Ret=false}}}if (!Ret)doCritCode(field crit msg)return(Ret)} // 判断是否是价格function is_price(field crit msg){var Ret = truevar NumStr= var decUsed=falsevar chrfor (i= i<field value length++i){chr=field value charAt(i)if (NumStr indexOf(chr )== ){if ( (!decUsed) &&chr== ){decUsed=true}else{Ret=false}}}if(Ret){if(decUsed&&(field value length field value indexOf( )<))else if(decUsed)Ret=false}if (!Ret)doCritCode(field crit msg)return(Ret)}
// 判断是否是空function is_null(field crit msg){var Text= + Trim(field value)if(Text length){ for(var i= i<Text lengthi++)if(Text charAt(i)!= &&Text charAt(i)!= )breakif(i>=Text length){Ret=true}else{Ret=false}}elseRet=trueif (Ret)doCritCode(field crit msg)return(Ret)}function IsSpace(field){var Text= +field valueif(Text length){for(var i= i<Text lengthi++)if(Text charAt(i)!= &&Text charAt(i)!= )breakif(i>=Text length)field value= }}
function doCritCode(field crit msg){if ( ( !=crit) ){alert(msg)if (crit== ){field focus()// focus does not work on certain netscape versionsfield select()}}}// 判断是否是整数function is_int(field crit msg){var Ret = truevar NumStr= var chrif (field value length== ){Ret=false}for (i= i<field value length++i){chr=field value charAt(i)if (NumStr indexOf(chr )== ){Ret=false}}if (!Ret)doCritCode(field crit msg)return(Ret)}// 判断是否是日期function is_date(field crit msg){var Ret = falsevar mark var mark var daysvar yvar mvar dif(field value== )return truecd=new Date()
if ( (mark = field value indexOf( ))== )mark =field value indexOf( )if (mark >){if ( (mark = field value indexOf( mark + )) == )mark =field value indexOf( mark + )if ((mark >)&&(mark + <field value length) ){y = parseInt(field value substring( mark ) )m = parseInt(field value substring(mark + mark ) )d = parseInt(field value substring(mark + field value length) )
year = new var_to_obj(y)month = new var_to_obj(m)day = new var_to_obj(d)days = getDaysInMonth(month value year value) +
if ((is_greater(day ))&&(is_less(day days))&&(is_greater(month ))&&(is_less(month ))&&(is_greater(year ))&&(is_less(year )))Ret=true}}if (!Ret) doCritCode(field crit msg)
return(Ret)}
function doCrit(field crit msg){if ( ( !=crit) ){alert(msg)if (crit== ){field focus()// focus does not work on certain netscape versions}}}// 判断是否有有效数据被选中function isselected(field crit msg){value= +field options[field selectedIndex] valueif(value== )Ret=falseelseRet=trueif (!Ret)doCrit(field crit msg)return(Ret)}
// 检查是否是字符// cCharacter 输入值function isCharacter( cCharacter ){var sFormat = ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
if( sFormat indexOf( cCharacter ) == ){return false}
return true}
//判断是否是数字和字母的组合function isChrandNum( cCharacter ){for(ilen= ilen<str lengthilen++){if(str charAt(ilen) <|| str charAt(ilen) >){if(str charAt(ilen) <a || str charAt(ilen) >z ){if(str charAt(ilen) <A || str charAt(ilen) >Z )return false}} }return true}
// 检查是否是其它可以作名称的字符// cCharacter 输入值function isOtherNameCharacter( cCharacter ){var sFormat = _
if( sFormat indexOf( cCharacter ) == ){return false}
return true}function isOtherNameCharacter ( cCharacter ){var sFormat =
if( sFormat indexOf( cCharacter ) == ){return false}
return true}
function isOtherNameCharacter ( cCharacter ){var sFormat =
if( sFormat indexOf( cCharacter ) == ){return false}
return true}
// 检查是否是可以作名称的字符// sValue 输入值function isNameCharacter( sValue ){if( sValue == null ){return false}
for( i = i <sValue lengthi ++ ){var cCharacter = sValue charAt( i )if( isDigital( cCharacter ) == false &&isCharacter( cCharacter ) == false &&isOtherNameCharacter( cCharacter ) == false&&isOtherNameCharacter ( cCharacter ) == false&&isOtherNameCharacter ( cCharacter ) == false ){return false}}
return true}//检查是否是密码function ispassword( sValue ){if( sValue == null ){return false}
for( i = i <sValue lengthi ++ ){var cCharacter = sValue charAt( i )if( isDigital( cCharacter ) == false &&isCharacter( cCharacter ) == false &&isOtherNameCharacter( cCharacter ) == false){return false}}
return true}
// 检查是否是Email
// sValue 输入值 合法格式为a@b c d此类形式function isEmail( sValue ){var iFirstIndex = var iSecondIndex = sValue indexOf( @ )if( iSecondIndex == ){return false}
var sTemp = sValue substring( iFirstIndex iSecondIndex )if( isNameCharacter( sTemp ) == false ){return false}
iSecondIndex = sValue indexOf( )if( iSecondIndex == || sValue substring( sValue length sValue length ) == ){return false}else if( sTemp length == sValue length ) // The last o characters are @ and {return false}else{var sTempValue = sValueiSecondIndex = sValue indexOf( @ )while( iSecondIndex != ){iFirstIndex = iSecondIndex + sTempValue = sTempValue substring( iFirstIndex sTempValue length )// The right section of valueiSecondIndex = sTempValue indexOf( )//document write( sTempValue= + sTempValue + <br>)sTemp = sTempValue substring( iSecondIndex )//document write( sTemp= + sTemp + <br>)if( isNameCharacter( sTemp ) == false ){return false}}
if( isNameCharacter( sTempValue ) == false ){return false}}
return true}
// 检查是否是邮编// sValue 输入值 合法格式为六位整数function isZIP( sValue ){if( sValue == null ){return false}
if( sValue length != ){return false}else{for( i = i <i ++ ){if( isDigital( sValue charAt( i ) ) == false ){return false}}}
return true}
// 检查是否是数字字符串// sValue 输入值function isDigitalString( sValue ){if( sValue == null ){return false}
for( i = i <sValue lengthi ++ ){if( isDigital( sValue charAt( i ) ) == false ){return false}}}
//IsEmpty函数判断一个字符串是否为空function IsEmpty(his){flag = truefor(var i= i<his lengthi++){if(his charAt(i)!= ){ flag = falsebreak}}return flag}//Trim函数去掉一字符串两边的空格function Trim(his){//找到字符串开始位置Pos_Start = for(var i= i<his lengthi++){if(his charAt(i)!= ){Pos_Start = ibreak}}//找到字符串结束位置Pos_End = for(var i=his length i>= i ){if(his charAt(i)!= ){Pos_End = ibreak}}//返回的字符串Str_Return = if(Pos_Start!= &&Pos_End!= ){ for(var i=Pos_Starti<=Pos_Endi++){Str_Return = Str_Return + his charAt(i)}}return Str_Return} //IsDigital函数判断一个字符串是否由数字(int or long)组成 function isDigital(str) {for(ilen= ilen<str lengthilen++){if(str charAt(ilen) <|| str charAt(ilen) >){return false} }return true}//IsFloat函数判断一个字符串是否由数字(int or long or float)组成 function IsFloat(str) {flag_Dec = for(ilen= ilen<str lengthilen++){if(str charAt(ilen) == ){flag_Dec++if(flag_Dec >)return falseelsecontinue} if(str charAt(ilen) <|| str charAt(ilen) >){return false} }return true}//IsTelephone函数判断一个字符串是否由数字或 * 组成 function IsTelephone(str) {for(ilen= ilen<str lengthilen++){if(str charAt(ilen) <|| str charAt(ilen) >){if((str charAt(ilen)!= )&&(str charAt(ilen)!= * ))return false} }return true}
//比较两个日期的大小 Num >Num return:trueNum <=Num return:falsefunction Compare_Date(Num Num ){var pos pos endvar para para para para para para
//para :年//para :月//para :日end=Num lengthpos =Num indexOf( )pos =Num indexOf( pos + )para =Num substring( pos )para =Num substring(pos + pos )para =Num substring(pos + end)para =parseInt(para )para =parseInt(para )para =parseInt(para )end=Num lengthpos =Num indexOf( )pos =Num indexOf( pos + )para =Num substring( pos )para =Num substring(pos + pos )para =Num substring(pos + end)para =parseInt(para )para =parseInt(para )para =parseInt(para )if(para >para ){return true}else if(para ==para ){if(para >para ){return true}else if(para ==para ){if(para >para ){return true}}}return false}
//去掉字符串所有空格function jtrimstr(str){var i= var jvar len=str lengthtrimstr= while(i<len){if(str charAt(i)!= ){trimstr=trimstr+str charAt(i)}i++}return(trimstr)}//转化日期function transferDate(str){var m= var strlen=str lengthvar n=strlen while (n>=strlen ){if(str charAt(n)== ){break}n=n }trimstr=str substring(m+ n)+ / + str substring(n+ strlen)+ / +str substring( m) return(trimstr)}
// 取得每月天数的函数//参数说明 month 月year 年// 返回值 days 天数function getDaysInMonth(month year) {var daysif (month== || month== || month== || month== || month== || month== || month== ) days= else if (month== || month== || month== || month== ) days= else if (month== ) {if (isLeapYear(year)) { days= }else { days= }}return (days)}// 判断是否为润年的函数//参数说明 Year 年份// 返回值:如果是润年 返回true 否则返回false
function isLeapYear (Year) {if (((Year % )== ) &&((Year % )!= ) || ((Year % )== )) {return (true)} else { return (false)}}
function isCharnum(str) {for(ilen= ilen<str lengthilen++){if(str charAt(ilen) <|| str charAt(ilen) >){if((str charAt(ilen)!= x ))return false} }return true}
function isCharsf(str) {for(ilen= ilen<str lengthilen++){if(str charAt(ilen) <|| str charAt(ilen) >){if(str charAt(ilen) <a || str charAt(ilen) >z )return false} }return true}
//判断一个字符串是否是由数字和 组成
function i *** onth(str) {for(ilen= ilen<str lengthilen++){if(str charAt(ilen) <|| str charAt(ilen) >){if((str charAt(ilen)!= ))return false} }return true}
lishixinzhi/Article/program/Java/Javascript/201311/25474