随笔分类 - javascript
摘要:$1,$2是表示的匹配的小括号里的内容 $1是匹配的第一个小括号里的 ,$2是匹配的第2个小括号里的 // 将yyyy-mm-dd格式转换为年月日格式 function chDate1date(str){ var reg =/(\d{4})\-(\d{2})\-(\d{2})/; return st
阅读全文
摘要:function once(fn) { let flag = false; let result = null; return function (...args) { if (flag) { return result; } flag = true; result = fn.call(this,
阅读全文
摘要:// arr:要查找的数组,predict:要查找的 key 字符串 或 [key,value] 数组,或 对象{key,value},fromIndex:要从数组中第一个元素开始查,默认为 0 function find(arr, predict, fromIndex = 0) { // 定义查找
阅读全文
摘要:// 自己实现function chunk(list, size) { let len = list.length; if (size < 1 || !len) { return []; } if (size > len) { return [list]; } // 最终返回数组 let res =
阅读全文
摘要:function isEqual(x, y) { // 如果全等则直接返回 true if (x y) { return true; } else if ( typeof x "object" && x !== null && typeof y "object" && y !== null ) {
阅读全文
摘要:两种实现方案实现原理:1. 如果传入的 function 为空,则直接返回 参数数组2. 如果传入的 function 只有一个,则直接调用第一个3. 否则从右向左依次执行4. 原理为:compose中传入多个函数,会依次从右向左执行,将右面函数的执行结果作为参数传入左边一个函数中 // 方式一 f
阅读全文
摘要:Object.create(),浅拷贝 const clone = Object.create( Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj) ); Object.assign(targetObj,sourceOb
阅读全文
摘要:参考链接:https://blog.csdn.net/lgno2/article/details/124996065 对象属性的可枚举和不可枚举 for in 循环只能遍历可枚举的,如果含有 Symbol ,则无法遍历,会报错 Uncaught TypeError: obj is not itera
阅读全文
摘要:reduce 方案 let myArray = ['a', 'b', 'a', 'b', 'c', 'e', 'e', 'c', 'd', 'd', 'd', 'd'] let myArrayWithNoDuplicates = myArray.reduce(function (previousVa
阅读全文
摘要:// 判断对象的方法 let obj6 = { a: 1, b: 2 }; // 1. typeof console.log(typeof obj6 "object"); // 2. instanceof console.log(obj6 instanceof Object true); // 3.
阅读全文
摘要:概念 1. 防抖(debounce):触发高频事件后 n 秒内函数只会执行一次,如果 n 秒内高频事件再次被触发,则重新计算时间举例:就好像在百度搜索时,每次输入之后都有联想词弹出,这个控制联想词的方法就不可能是输入框内容一改变就触发的,他一定是当你结束输入一段时间之后才会触发。 2.节流(thro
阅读全文
摘要:console.log("array flat"); // 1. 因为只有数组才有 concat 方法,所以这里必须写入初始值 空数组 只能打平一层 // const flat = (list) => list.reduce((a, b) => a.concat(b), []); // 2. 直接用
阅读全文
摘要:1. Array.isArray([]) // true 2. Object.prototype.toString.call([]) // '[object Array]' 3. [].constructor Array // true 4. [] instanceof Array // true
阅读全文
摘要:function reduce(list, fn, ...init) { let prev = init.length > 0 ? init[0] : list[0]; for (let i = init.length > 0 ? 0 : 1; i < list.length; i++) { pre
阅读全文
摘要:// promise.all function promiseAll(promisesArr) { return new Promise((resolve, reject) => { // 定义一个计数器 let count = 0; const arr = []; const l = promis
阅读全文
摘要:console.log(" sleep "); // sleep 等待几秒 const sleep = (seconds) => new Promise((resolve) => setTimeout(resolve, seconds)); async function sleepTest() {
阅读全文
摘要:// bind会返回一个硬绑定的新函数,新函数会使用指定的第一个thisCtx去调用原始函数,并将其它参数传给原始函数。 硬绑定会降低函数的灵活性,在绑定之后不能通过显式或硬绑定的方式改变this,只能通过new改变 // softBind 会对指定的函数进行封装,首先检查调用时的 this,如果
阅读全文
摘要:自从开了这个公众号,一直没时间为大家分享技术文章,今天得空,来开启的我首篇发文。 首先说,我也是一个前端小白,以后且叫我小叙妹妹就可,刚刚步入前端行业半年时间,很多东西都还在不断学习中,所以以后公众号为大家分享的,大部分都是一些个人学习总结的笔记与心得,如果看到好的技术文章,也会为大家分享出来(当然
阅读全文
摘要:// 1. ? var obj ={a:1,b:2} obj.c.a // 会报错:Uncaught TypeError: Cannot read properties of undefined,因为obj.c 不存在 解决方法: obj.c?.a // 等价于 obj.c && obj.c.a /
阅读全文