hwb2017

Results 12 comments of hwb2017

刚看到7天实现Web框架,大佬又出新品啦

[codepen demo](https://codepen.io/hwb2017/pen/vYZPPVw) ```javascript const allElements = document.querySelectorAll("*") const elementFrequency = Array.from(allElements).reduce((a,b) => { a[b.tagName] = a[b.tagName] ? a[b.tagName]+1 : 1 return a }, {}) console.log(elementFrequency) const sortedElementFrequency = Object.entries(elementFrequency).sort((a, b)...

```typescript const lodashGet = (object: { [key: string]: any }, path: Array | string, defaultValue?: any): any => { let result: any const findArrayPath = (path: Array): any => {...

最基础功能的实现: ```javascript const encode = (str) => { const encodedArray = Array.from(str).reduce((a,b) => { if (a.length === 0) { a.push(b, 1) return a } let lastChar = a[a.length - 2]...

方案二的简单Demo: ```html 图片懒加载 img { width: 100%; height: 600px; } const images = document.querySelectorAll('img'); const lazyLoad = () => { images.forEach((item) => { // 触发条件为img元素的CSSOM对象到视口顶部的距离 < 100px + 视口高度,+100px为了提前触发图片加载 if...

方案二的Demo(CodePen) https://codepen.io/hwb2017/pen/BaZKeLa

通过svg实现的简单Loading动画 \ https://codepen.io/hwb2017/pen/XWgNVyr

Promise.any 的行为跟 Promise.all 刚好相反 ```javascript Promise.any = (promiseArray) => { return new Promise((resolve, reject) => { const _promiseArray = Array.from(promiseArray) const length = _promiseArray.length const rejectedArray = [] _promiseArray.forEach(item =>...

双指针法获取所有组合 ```javascript const twoSum = (arr, sum) => { if (arr.length sum) { right-- } else { left++ } } return result } ```

不用栈,全部用正则实现 ```javascript const countOfLetters = (str) => { let frequencyMap = {} let regArray = [ /([a-zA-Z])([a-zA-Z])/g, //AA /([a-zA-Z])(\))/g, //A) /([a-zA-Z])(\()/g, //A( /(\))([a-zA-Z])/g, //)A /(\))(\))/g, //)) /(\))(\()/g, //)( ] let...