高阶函数之函数作为参数使用 高阶函数之函数作为返回值使用 函数作为参数的练习
生活随笔
收集整理的這篇文章主要介紹了
高阶函数之函数作为参数使用 高阶函数之函数作为返回值使用 函数作为参数的练习
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
高階函數之函數作為參數使用
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Document</title><script>// function f1(fn){// console.log("f1的函數");// // 此時fn當成是一個函數來使用的// fn();// }// fn是參數,最后作為函數使用了,函數是可以作為參數使用// 傳入匿名函數// f1(function(){// console.log("我是匿名函數");// });// 命名函數// function f2(){// console.log("f2的函數");// }// f1(f2);// 函數作為參數的時候,如果是命名函數,那么只傳入命名函數的名字,沒有括號function f1(fn){setInterval(function(){console.log("定時器開始");fn();console.log("定時器結束");},1000);}// f1(function(){// console.log("好困,好餓");// });</script> </head> <body></body> </html>高階函數之函數作為返回值使用
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Document</title><script>// function f1(){// console.log("f1函數開始");// return function(){// console.log("我是函數,但是此時是作為返回值使用的");// }// console.log("f1函數結束");// }// var ff = f1();// ff();// var num = 10;// // 獲取num這個變量的數據類型// console.log(typeof num);// // 對象// var obj = {};// console.log(obj instanceof Object);// // 獲取某個對象的數據類型的樣子// // 此時得到的就是這個對象的類型的樣子// // Object.prototype.toString.call(對象);// // 此時輸出的是Object的數據類型// console.log(Object.prototype.toString());// // 輸出數組的數據類型 [Object Array]// console.log(Object.prototype.toString.call([]));// var arr = [10,20];// console.log(Object.prototype.toString.call(arr));// console.log(Object.prototype.toString.call(new Date()));// 獲取某個對象的類型是不是你傳入的類型// [10,20] 是不是[object Array]// type----是變量---是參數----"[object Array]"// obj----是變量----是參數----[10,20]// 判斷這個對象和傳入的類型是不是同一個類型function getFunc(type){return function(obj){return Object.prototype.toString.call(obj)===type;}}// var ff = getFunc("[object Array]");// var result = ff([10,20]);// console.log(result);var ff = getFunc("[object Date]");var result = ff(new Date());console.log(result);</script> </head> <body></body> </html>函數作為參數的練習
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Document</title><script>// var arr = [1,5,8,63,14,6,45];// // 排序// arr.sort();// console.log(arr);var arr = [1,5,8,63,14,6,45];// // 排序--函數作為參數使用,匿名函數作為sort方法的參數使用// // 那么此時的匿名函數中有兩個參數,arr.sort(function(obj1,obj2){if(obj1>obj2){return 1;}else if(obj1==obj2){return 0;}else{return -1;}});console.log(arr);var arr = ["acdf","abcd","bcedf","bced"];arr.sort(function(a,b){if(a>b){return 1;}else if(a==b){return 0;}else{return -1;}});console.log(arr);</script> </head> <body></body> </html>?
總結
以上是生活随笔為你收集整理的高阶函数之函数作为参数使用 高阶函数之函数作为返回值使用 函数作为参数的练习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 总结apply和call方法的使用 bi
- 下一篇: 函数作为返回值练习 作用域和作用域链及预