Skip to content

常用代码片段

禁止打印、截图、复制和粘贴
Date日期转字符串
日期字符串转时间戳
时间戳转日期Date
全屏显示(某个DOM)
数字增加千分符号
个位数高位补0
js
// 个位数补两位数(字符串)
// 例:'1'-'9' => '01'-'09'
let num = '1'
num = ('00' + num).substr(num.length)
范围内随机数
js

function selectFrom(lowerValue, upperValue) {    
    let choices = upperValue - lowerValue + 1;   
    return Math.floor(Math.random() * choices + lowerValue); 
} 
let num = selectFrom(2,10); 
console.log(num);  // 2~10范围内的值,其中包含2和10 


// 随机数公式
// total: 总共的数字有几个
// lowest:最小的数字
let number = Math.floor(Math.random() * total + lowest)
// 例:[1-10],总共10个数字,最小1
number = Math.floor(Math.random() * 10 + 1)  // [1-10]
动态加载js和css

代码演示


手写实现

手写new
手写apply、call、bind
手写浅拷贝
手写深拷贝
手写Promise
手写数组迭代器interator
手写实现JSON.stringify
手写数组扁平化flat
手写数组push方法
手写数组pop方法
手写数组map方法
手写数组reduce方法
手写数组forEach方法
手写数组filter方法
手写EventEmitter