1.0 随机数的获取 返回true/false
//true/false 概率各50%
const randomBoolean =()=> Math.random() >0.5
console.log(randomBoolean())
2.0 判断时间是否是工作日
const isWorkDay = (date)=>date.getDay() %6 !==0
console.log(isWorkDay(new Date(2021,1,23)))//false
console.log(isWorkDay(new Date(2021,1,25)))//true
3.0 反转字符串
const reverse = (str)=>str.split('').reverse().join('')
console.log(reverse('helloworld')) //dlrowolleh
4.0 检查当前tab 是否在前台 (document.hidden)
const isBowserInterview = () => document.hidden;
console.log(isBowserInterview()) //控制台打印 返回的为false
5.0 检查数字是否是奇数
const isEven =num => num%2===0
console.log(isEven(2)) //true
console.log(isEven(3)) //false
6.0 从日期中获取时间 (toTimeString()方法)
const isDateStr = date => date.toTimeString().slice(0,8)
console.log(isDateStr(new Date(2021,1,25,11,06,0))) //11:06:00
7.0 检查当前用户是否是苹果设备 (非苹果返回false,是苹果为true)
const isAppleDevicer = /Mac|iPod|iPhone|iPad/.test(navigator.platform);
console.log(isAppleDevicer)
8.0 滚动页面到顶部
const goToTop = ()=> window.scrollTo(0,0)
goToTop () //调用该方法 页面即滚动到顶部
9.0 获取所有参数的平均值
const average = (...args)=>args.reduce((a,b)=>a+b)/(args.length)
console.log(average(1,2,3,4)) //2.5
10.if 多条件判断
function X(x){
// if(x==='abc'||x==='def' || x==='ghk'){
// console.log(x)
// }
//优化之后
if(['abc','def','hjk'].includes(x)){
console.log(x)
}
}
let x = new X("abc") //abc
let x1 = new X("def") //def
let x2 = new X("hjk") //hjk
11.if ... else
let a =10
// if(a>10){
// test = false
// }else{
// test = true
// }
//优化之后
let test = a>10;
console.log(test) //true
12. 函数调用
function test1(){
console.log('test1')
}
function test2(){
console.log('test2')
}
var test3 =1
// if(test3===1){
// test1()
// }else{
// test2()
// }
test3 === 1? test1():test2(); //test1
等价于
(test3===1?test1:test2)() //test1
网友评论