
前端面试
lucky__peng
这个作者很懒,什么都没留下…
展开
-
js手写bind
Function.prototype.bind = function(context, ...args) { if (typeof this !== 'function') throw new TypeError('error'); const that = this; return function fn() { if (this instanceof fn) { return new that(...args, ...arguments); } else {原创 2021-04-11 16:10:47 · 74 阅读 · 0 评论 -
js手写apply
Function.prototype.apply = function(thisArg = window, args) { if (typeof this !== 'function') throw ('not a function'); thisArg.fn = this; let res = thisArg.fn(...args); delete thisArg.fn; return res;}原创 2021-04-11 13:06:01 · 136 阅读 · 0 评论 -
js深拷贝
(一) 先来看下有点问题的写法// JSON.parse()与JSON.stringify()const clone = function (obj) { return JSON.parse(JSON.stringify(obj));}这种方法只适用于纯数据json对象的深度克隆,缺陷参考如下代码:const clone = function (obj) { return JSON.parse(JSON.stringify(obj));}let a = {a:functio原创 2021-04-09 10:34:35 · 114 阅读 · 0 评论 -
jsreduce实现
Array.prototype.reduce = function(fn, initValue) { if (typeof fn !== 'function') throw ('Error in params'); let arr = this, acc = arr[0], i = 1; if (arguments.length !== 1) { acc = initValue; i = 0; } for(; i < arr.length; i++) acc = .原创 2021-04-08 00:39:37 · 225 阅读 · 0 评论 -
js类数组转化为数组
原创 2021-04-05 23:30:46 · 114 阅读 · 0 评论 -
jsforEach实现
Array.prototype.forEach = function(fn, thisArgs) { if (typeof fn !== 'function') throw ('Error in params'); let arr = this; for (let i in arr) fn.call(thisArgs, arr[i], i, arr);}原创 2021-04-05 23:25:25 · 377 阅读 · 0 评论 -
jsmap实现
Array.prototype.map = function(fn, thisArgs) { if (typeof fn !== 'function') throw ('Error in params'); let res = [], arr = this; for (let i in arr) { res[i] = (fn.call(thisArgs, arr[i], i, arr)); } return res;}原创 2021-04-05 10:39:19 · 81 阅读 · 0 评论 -
手写filter
Array.prototype.filter = function(fn, thisArgs) { if (typeof fn !== 'function') throw ("Error in params"); let res = [], arr = this; for (let i in arr) { if (fn.call(thisArgs, arr[i], i, arr)) res.push(arr[i]); } return res;}原创 2021-04-04 14:31:16 · 137 阅读 · 0 评论 -
函数柯里化实现
function add() { const args = [...arguments]; function _add() { args.push(...arguments); return _add; } _add.toString = () => args.reduce((pre,cur)=>pre+cur); return _add; }原创 2021-04-03 23:05:10 · 115 阅读 · 0 评论 -
js数组去重
// 对Map方法的补充,可以用来去重 NaN 和 对象function unique(arr){ let map=new Map() let array=new Array() for(let i=0;i<arr.length;i++){ if (typeof arr[i]==='object') { // 数组元素是对象的情况 let a=JSON.stringify(arr[i]) if(!map.has(a)){ m原创 2021-04-01 09:36:13 · 92 阅读 · 0 评论 -
js函数call实现
Function.prototype.call = function(context = window, ...args) { if (typeof this !== 'function') throw new TypeError(); context.fn = this; let res = context.fn(...args); delete context.fn; return res;}原创 2021-04-01 09:29:22 · 313 阅读 · 0 评论 -
节流throttle实现
const throttle = function(fn, time) { let flag = true; return function() { if (!flag) return; flag = false; setTimeout(() => { fn.apply(this, arguments); flag = true; }, time) }}原创 2021-03-29 23:31:08 · 142 阅读 · 0 评论 -
Debounce()防抖函数实现
const Debounce = (fn, wait) => { let timer; return () => { if (timer) clearTimeout(timer); timer = setTimeout(() => { fn.apply(this, arguments); }, wait) }}原创 2021-03-29 10:26:30 · 263 阅读 · 0 评论 -
数组扁平化
原创 2021-03-28 11:10:54 · 85 阅读 · 0 评论 -
js前端面试常考
数组扁平化数组去重类数组转化为数组 (类数组是具有length属性,但不具有数组原型上的方法)array.prototype.filter()Array.prototype.map()Array.prototype.forEach()Array.prototype.reduce()Apple() (function.prototype.apple())callBind()Debounce()防抖 (频率高频时间后n秒内函数只会执行一次,如果n秒内高频时间再次触发,则重新计算时间)t.原创 2021-03-28 10:51:41 · 229 阅读 · 3 评论