![](https://img-blog.csdnimg.cn/20201014180756922.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
手写
面试常见的手写代码
Novice-XiaoSong
这个作者很懒,什么都没留下…
展开
-
javascript 深拷贝详解(多版本深拷贝)
简单对象的深复制(不含函数) let newObj = JSON.parse(JSON.stringify(obj)); 递归实现复杂对象的深复制 let deepCopy = function(_obj){ /** * 对复杂对象进行深复制 */ let result = typeof _obj === 'list'? []: {}; for(let key in _ob...原创 2020-11-30 17:00:12 · 202 阅读 · 0 评论 -
数据结构 使用2个栈实现一个队列
function StackQueue(){ // 数组模拟栈,只能用push, pop方法 let stack1 = []; // 用于入队 let stack2 = []; // 用于出队 this.size = 0; this.push= function(_item){ stack1.push(_item); this.size++; } this.pop = function(){ if(stack2.length <= 0){ // 需要构建stack2原创 2020-11-18 00:27:13 · 198 阅读 · 0 评论 -
javascript 使用function*/yield模拟简单模式的async/await
function* 模拟 async yield 模拟 await 简单模式:yield 后边只能跟着 promise,且不进行异常处理 function asyncFunc(){ function run(_genFunc){ /* * 启动器 * @param {Generator Function} _genFunc: 生成器函数 */ return new Promise(resolve=>{ let gen = _genFunc(); functi.转载 2020-11-18 10:59:18 · 329 阅读 · 0 评论 -
javascript 防抖、节流
一、防抖 实现 function doSomething(){ console.log(document.body.scrollTop); } /** * 防抖函数 debounce * 在第一次触发操作时,延迟操作;后续触发操作时,判断当前是否有timer,有的话直接重置timer,进而实现防抖 * @param { Function } _fn: 事件触发后要执行的操作 * @param { Number } _delay: 延时(ms) * @return { Function }:转载 2020-10-27 13:01:21 · 102 阅读 · 0 评论 -
数据结构 实现堆
堆分类:最大堆、最小堆 堆别名:优先队列 堆应用:求topK,堆排序 堆模板 function Heap(_arr, _compareFunc){ // 私有属性 let heap = []; // 对象公有属性 this.size = 0; // 私有方法 // 计算左、右、父节点索引 let left = () => {}; let right = () => {}; let parent = () => {}; // 堆的shiftU.原创 2020-11-03 11:55:27 · 135 阅读 · 0 评论 -
javascript 模拟new的实现
引用MDN对new运算符的定义: new 运算符创建一个用户定义的对象类型的实例或具有构造函数的内置对象的实例。 MDN: new操作符会干下面这些事: (1) 创建一个空的简单JavaScript对象(即{}) (2) 链接该对象(即设置该对象的构造函数)到另一个对象 (3) 将步骤1新创建的对象作为this的上下文 (4) 如果该函数没有返回对象,则返回this 我稍微重新解释下2,转为实现步骤: (1) 创建obj (2) 链接类的原型到obj的原型上 (3) 在obj中创建类的对象作用域 (4)..原创 2020-11-10 12:07:33 · 150 阅读 · 0 评论 -
手写 实现call(), apply()
一、call() ES6之前 Function.prototype.myCall = function (_context){ // _context空检测 _context = _context? _context: window; // 获取函数参数 let args = []; for(let i=1, len=arguments.length; i<len; i++){ // 避开在eval时组装参数中字符串的麻烦 args.push('arguments[' + i +原创 2020-10-31 02:49:51 · 133 阅读 · 0 评论 -
手写 实现bind()
逐步实现js的bind方法 一. 无参数版 bind ES6之前 Function.prototype.myBind = function(_context){ // _context检测 _context = _context? _context: window; let that = this; // 按要求返回闭包(函数) return function(){ // 函数挂入_context内 _context.__fn = that; // 函数调用 let res.原创 2020-10-31 12:15:15 · 814 阅读 · 0 评论