手写代码
沿着路走到底
涉及前端,后端,数据库
展开
-
TaskPool 类
【代码】TaskPool 类。原创 2022-08-24 16:23:05 · 124 阅读 · 0 评论 -
Event 类
【代码】Event 类。原创 2022-08-24 16:20:49 · 130 阅读 · 0 评论 -
JS车牌校验
1const vehiclePlateNumber = value.toUpperCase() if (vehiclePlateNumber.length != 7 && vehiclePlateNumber.length != 8) { return false; } var regExp1 = /(^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领]{1}[A-Z]{1}[0-9A-Z]{5}$)/ if (vehi原创 2021-11-23 17:49:40 · 549 阅读 · 0 评论 -
手写 instanceof
1function myInstance(left, right) { var proto = left.__proto__; var prototype = right.prototype; if (proto === null) { return false; } else if (proto === prototype) { return true; } else { return myInstance(p原创 2021-11-16 13:08:13 · 193 阅读 · 0 评论 -
判断链表是否有环
141使用快慢指针var hasCycle = function(head) { if (!head || !head.next) return false let slow = head.next let fast = head.next.next while (slow !== fast) { if(!fast || !fast.next) return false slow = slow.next fast =原创 2021-11-14 14:21:04 · 183 阅读 · 0 评论 -
比较版本号
1export function compareVersion(version1:string, version2:string) { const v1 = version1.split('.') const v2 = version2.split('.') const len = Math.max(v1.length, v2.length) // 调整两个版本号位数相同 while (v1.length < len) { v1.push('0') } whi原创 2021-11-04 09:38:16 · 62 阅读 · 0 评论 -
统计数组里出现次数最多的字母
1const maxLetter = function(str) { const temp = {} const arr = str.split('') for(const item of arr) { if (item in temp) { temp[item]++ } else { temp[item] = 1 } } let max = 0 for(const item in temp) { if(temp[原创 2021-11-02 16:27:17 · 256 阅读 · 1 评论 -
手写字符串倒序
1const reverse = function(str) { const arr = str.split('') let start = 0 let end = arr.length - 1 while (start <= end) { const temp = arr[start] arr[start] = arr[end] arr[end] = temp start++ end-- } return ar原创 2021-11-02 16:13:03 · 59 阅读 · 0 评论 -
JS 快速排序
1const quickSort = function(arr) { if (arr <= 1) return arr const middleIndex = Math.floor(arr.length / 2) const middle = arr.splice(middleIndex, 1)[0] const left = [] const right = [] for(const item of arr) { if (item <原创 2021-11-02 15:22:34 · 77 阅读 · 0 评论 -
二叉树深度
1/** * Definition for a binary tree node. * function TreeNode(val, left, right) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this.right = (right===undefined ? null : right) * } *//** *原创 2021-11-02 14:56:17 · 3366 阅读 · 0 评论 -
数组扁平化
1const arr = [1, [2, 3], [4, 5], 6,[7, [8, 9, [10]]], 11]function flatten(arr){ let flatArr = [] for(let item of arr) { if (Array.isArray(item)) { flatArr = flatArr.concat(flatten(item)) } else { flatArr.push(item) } }原创 2021-11-02 14:17:20 · 54 阅读 · 0 评论 -
JS数组去重
1const arr = [1,0,0,2,9,8,3,1]console.log([...new Set(arr)]) // [1,0,2,9,8,3]function unique(arr) { const temp = [] for(const item of arr) { if (temp.indexOf(item) === -1) { temp.push(item) } } return temp}console.log(unique(a原创 2021-11-02 13:10:07 · 57 阅读 · 0 评论 -
柯里化函数
1// 柯里化之前 function add(x, y) { return x + y; }// 柯里化之后function curry(y) { return function(x) { return x + y }}console.log(add(1, 2) ) // 3console.log(curry(2)(1)) // 3下面的例子,这里我们定义了一个 add 函数,它接受一个参数并返回一个新的函数。调用 add 之后,返回的函数就通过闭包.原创 2021-11-02 12:52:43 · 69 阅读 · 0 评论 -
判断是否回文
1function isPalindrome(str) { const newVal = str.split('').reverse().join('') return str === newVal}console.log(isPalindrome('abcdedcba'))1原创 2022-02-22 15:59:52 · 40 阅读 · 0 评论 -
手写 promise
const PENDING = 'pending'const FULFILLED = 'fulfilled'const REJECTED = 'rejected'class MyPromise { constructor(executor) { try { executor(this.resolve, this.reject) } catch (e) { this.reject(e) } } // promise 状态 statu..原创 2021-04-25 09:57:55 · 93 阅读 · 0 评论 -
手写bind
bind 基础用法function fn (a, b, c) {} const f = fn.bind(context, 1, 2 ,3)f() const f1 = fn.bind(context, 1, 2)f1(3) const f2 = fn.bind(context, 1)f2(2, 3)手写bindFunction.prototype.mybind = function (context, ...args) { return (...rest原创 2021-04-25 09:56:22 · 76 阅读 · 0 评论 -
手写一个new
functionDog(name){this.name=name}Dog.prototype.bark=function(){console.log('wangwang')}Dog.prototype.sayName=function(){console.log('mynameis'+this.name)}function_new(fn,...arg...原创 2020-12-22 17:12:50 · 86 阅读 · 0 评论 -
算法手写题
1、已知如下数组:var arr = [ [1, 2, 2], [3, 4, 5, 5], [6, 7, 8, 9, [11, 12, [12, 13, [14] ] ] ], 10];编写一个程序将数组扁平化去并除其中重复部分数据,最终得到一个升序且不重复的数组const arr = [ [1, 2, 2], [3, 4, 5, 5], [6, 7, 8, 9, [11, 12, [12, 13, [14] ] ] ], 10]const arr1 = [...new Set(arr.原创 2020-12-22 15:18:12 · 163 阅读 · 0 评论 -
广度优先遍历
<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>Document</title></head><body><divid="d...原创 2020-12-22 11:00:45 · 58 阅读 · 0 评论 -
深度优先遍历
<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width,initial-scale=1.0"><title>Document</title></head><body><divid="d...原创 2020-12-22 10:49:25 · 61 阅读 · 0 评论 -
深拷贝
//1.不考虑函数letobj2=JSON.parse(JSON.stringify(obj1))//2functiondeepClone(obj){if(typeofobj!=="object"||obj===null){returnobj}letresultif(Array.isArray(obj)){result...原创 2020-12-21 16:54:53 · 70 阅读 · 0 评论 -
节流
functionthrottle(fn,delay=200){lettimer=nullreturnfunction(){if(timer){return}timer=setTimeout(()=>{fn()timer=null},delay)}}...原创 2020-12-21 16:40:51 · 280 阅读 · 0 评论 -
防抖
functiondebounce(fn,delay){lettimer=nullreturnfunction(){clearTimeout(timer)timer=setTimeout(()=>{fn()},delay)}}...原创 2020-12-21 16:33:36 · 74 阅读 · 0 评论