js

共找到 25 篇文章

25 篇文章
20 分钟阅读
37500 字数
js

SharedArrayBuffer is not defined

SharedArrayBuffer is not defined 如何解决 🔗SharedArrayBuffer 是一种 JavaScript 对象,用于在多线程环境中共享内存。然而,由于共享内存的特性,它可能导致安全漏洞。

js

请求中获取浏览器推荐语言

请求中获取浏览器推荐语言 🔗 1 2 3 4 5 6 7 8 9 10 11 12 let languages: string[] | undefined // get locale from cookie const localeCookie = request.cookies.get('locale') languages = localeCookie?.value ? [localeCookie.value] : [] if (!languages.length) { // Negotiator expects plain object so we need to transform headers const negotiatorHeaders: Record<string, string> = {} request.headers.forEach((value, key) => (negotiatorHeaders[key]

数据结构

双向链表

双向链表 🔗 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

数据结构

堆和栈

堆和栈 🔗Queue 🔗 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 import LinkedList from "./LinkedList"; class Queue { constructor() { this.linkedList = new LinkedList(); } isEmpty() { return !this.linkedList.head; } peek() { if (this.isEmpty()) return null; return this.linkedList.head.value; } enqueue(value) { this.linkedList.prepend(value); } dequeue() { return this.linkedList.deleteHead();

数据结构

单向链表

链表 🔗链表是一种物理储存上非联系,数据元素的逻辑顺序通过链表中的指针链接次序,实现的一种线性储存结构。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

js

简易的事件监听EventBus

简易的事件监听 🔗 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 const createEvent = () => ({ events: {} as any, on(eventName: string, callback: Function) { this.events[eventName]?.push(callback) || (this.events[eventName] = [callback]); return () => { this.events[eventName] = this.events[eventName].filter( (e: any) => e !== callback ); }; }, emit(eventName: string, ...args: any[]) { const callbacks = this.events[eventName];

js 获取滚动元素

js 获取滚动父元素 🔗 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 const isScrollable = function (ele) { const hasScrollableContent = ele.scrollHeight > ele.clientHeight; const overflowYStyle = window.getComputedStyle(ele).overflowY; const isOverflowHidden = overflowYStyle.indexOf('hidden') !== -1; return hasScrollableContent && !isOverflowHidden; }; const getScrollableParent = function (ele) { return !ele || ele === document.body ? document.body : isScrollable(ele) ? ele

canvas requestAnimationFrame画一个clock

canvas requestAnimationFrame 画一个 clock 🔗 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame; var h1 = document.getElementsByTagName("h1")[0]; var canvas = document.getElementById("canvas"); var ctx

js

mousedown event中保持input的focus状态

mousedown event中保持input的focus状态 🔗一定要使用e.preventDefault去除默认失去焦点行为 1 2 3 <input> <button>CLICK</button> <button>MOUSEUP</button> <button>MOUSEDOWN</button> <button>MOUSEDOWN 2</button> 1 2 3 4 5 6 7

js

比较两个数组的不同项

比较两个数组的不同项 🔗如何比较两个数组的不同项,并且取出组成一个新的数组 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 function diffArray(one, two) { if

Lerna 包管理器命令行

1 2 3 4 5 6 7 8 9 10 11 12 13 14 $ npm install lerna -g $ mkdir lerna-demo $ cd lerna-demo && lerna init --independent # 用的默认的固定模式,vue babel等都是这个 $ cd packages $ mkdir moduleA && mkdir moduleB ... #分别进入三个

Classnames库代码解读

classnames库是日常工作中用来操作dom中class相关的工具函数,这里我们对它进行解读 使用 npm install classnames –save 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

Lodash ToNumber

toNumber 🔗lodash中转数字函数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

数字加减问题解决精度问题

1 2 3 4 5 6 7 8 9 10 11 function plus(num1, num2) { const num1Digits = (num1.toString().split('.')[1] || '').length; const num2Digits = (num2.toString().split('.')[1] || '').length; const baseNumber = Math.pow(10, Math.max(num1Digits, num2Digits)); return (num1 * baseNumber + num2 * baseNumber) / baseNumber; } function minus(num1, num2) { return plus(num1, -1 * num2); }

判断是不是DOM元素

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 function isElement(obj: any) { try { // Using W3 DOM2 (works for FF, Opera and Chrome) return obj instanceof HTMLElement; } catch (e) { // Browsers not supporting W3 DOM2 don't have HTMLElement and // an exception is thrown and we end up here. Testing some // properties that all elements have (works on

Classname Operation

操作相关classname工具函数 1 2 3 4 5 6 7 8 9 10 11 // 增加目标class function addClass(target = '', srcCls = '') { const clss = target.split(/\s+/) return [...clss.filter(cls => !clss.includes(cls)), srcCls] } function removeClass(target = '', srcCls = '') { const clss = target.split(/\s+/) return [...clss.filter(cls

js

事件总线EventBus

日常我们在vue开发的时候使用公共事件总线进行跨组建数据传递,这里我们自己封装一个事件总线,了解其背后的原理 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

webpack

Webpack Loader Plugin

webpack Loaders and Plugins different 🔗Loaders: 🔗Loaders work at the individual file level during or before the bundle is generated. loader是文件加载器,能够加载资源文件,并对这些文件进行一些处理,

防抖与节流

no-refresh-page 🔗 history.pushState( {}, null, this.$route.path + ‘#’ + encodeURIComponent(params) )

HTML Element API 用法

Element.append 🔗方法在 Element的最后一个子节点之后插入一组 Node 对象或 DOMString 对象。 被插入的 DOMString 对象等价为 Text 节点。 Element.append((Node or DOMString)… nodes); 示例代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14

阅读全文
1000 字

常用判断函数

是否是promise 🔗 1 2 3 export function isPromise(object: any): object is Promise<any> { return Promise.resolve(object) === object; }

BaseFindIndex

baseFindIndex 🔗查找数组中的索引 1 2 3 4 5 6 7 8 9 10 11 function baseFindIndex(array, predicate, fromIndex, fromRight) { const { length } = array let index = fromIndex + (fromRight ? 1 : -1) while ((fromRight ? index-- : ++index < length)) { if (predicate(array[index], index, array)) { return index } } return -1 }

ArrayEach

ArrayEach实现循环 🔗用while中的break实现原来Array.prototype.forEach未实现的打断功能 1 2 3 4 5 6 7 8