<script>// forEach()functionrun1(){var x =[1,2,3,4,5,7,8,9];
x.forEach(function(each){
console.log(each);});}// map()functionrun2(){let arr =[1,2,3,4,5];const arr_res = arr.map(y => y *2);// const arr_res = arr.map(x => x * 2);
console.log(arr_res);}// for each in all like python that method!functionrun3(){let arr =[1,2,3,4,5];for(each in arr){
console.log(each);}}// every somefunctionrun4(){// everyvar arr =[1,6,8,-2,-5,7,-4]var isPositive = arr.every(function(value){return value >0;})
console.log(isPositive)// false//somevar arr =[1,6,8,-2,-5,7,-4]var isPositive = arr.some(function(value){return value >0;})
console.log(isPositive)// true}// do not speak it ! function reduce is so awesome! error word:aweasome!functionrun5(){var arr =[1,2,3,4,5,6]
console.log(arr.reduce(function(v1, v2){return v1 + v2;}))// 21//开始是1+2 = 3,之后3+3 =6,之后6+4 =10,之后//10+5 =15,最后15+6 =21
console.log(arr.reduce(function(v1, v2){return v1 - v2;},100))// 79//开始的时候100-1=99,之后99-2=97,之后97-3=94,之后//94-4=90,之后90-5=85,最后85-6=79}// lambda 而在es6的箭头函数出现后,lambda表达式就成了开发时的必备技能了// 当然最主要的原因也是因为这个方便了我们使用一些回调函数//例如// [1,2,3].reduce((a,b)=>a+b)//6// [1,2,3].map(a=>a*2)//2,4,6// ...functionrun6(){//例如letmax=(a, b)=> a > b ? a : b;//等于letmax=function(a, b){return a > b ? a : b;}}functionrun7(){}functionrun8(){}</script>
<!-- https://www.jianshu.com/p/4656c3cc97a8 --><script>// 初体验// 匿名函数(lambda)格式如下// function (x){ return x*x; }functionrun1(){varadd=(a, b)=> a + b;alert(add(3,5));}// Array.from() 随机生成functionrun2(){let res = Array.from({
length:5}, Math.random);
console.log(res);}// 那么 Array.from({length: 5},(v,i)=>i) 就相当于 Array.from({length: 5}).map((v,i)=>i)functionrun3(){let res_arr = Array.from({
length:10},(v, i)=> i);
console.log(res_arr);}// 显示错误,待续functionrun4(){constrgb2hex=([r, g, b])=>`#${(1<<24)+(r <<16)+(g <<8)+ b}`.toString(16).substr(1);// let res = rgb2hex([76, 11, 181]);
console.log(rgb2hex([76,11,181]));// #4c0bb5}functionrun5(){consthex2rgb= hex =>[1,3,5].map((h)=>parseInt(hex.substring(h, h +2),16));let res =hex2rgb("#4c0bb5");
console.log(res);// [76, 11, 181]}// # judge it is odd or even odd 奇数functionrun6(){const value =232;if(value &1) console.log("odd");else console.log("even");// even}functionrun7(){constfromAgo=(date)=>{const ms = Date.now()- date.getTime();const seconds = Math.round(ms /1000);const minutes = Math.round(ms /60000);const hours = Math.round(ms /3600000);const days = Math.round(ms /86400000);const months = Math.round(ms /2592000000);const years = Math.round(ms /31104000000);switch(true){case seconds <60:return`${seconds} second(s) ago"`;case minutes <60:return`${minutes} minute(s) ago"`;case hours <24:return`${hours} hour(s) ago"`;case days <30:return`${days} day(s) ago`;case months <12:return`${months} month(s) ago`;default:return`${years} year(s) ago`;}};const createdAt =newDate(2021,0,5);let res =fromAgo(createdAt);// 14 day(s) ago;alert(res)}// 8. 通过传参生成路径functionrun8(){constgeneratePath=(path, obj)=>
path.replace(/(\:[a-z]+)/g,(v)=> obj[v.substr(1)]);const route ="/app/:page/:id";generatePath(route,{
page:"products",
id:85,});// /app/products/123}run7()</script>