Skip to content

Commit

Permalink
feat: 补充面试题
Browse files Browse the repository at this point in the history
  • Loading branch information
dobble11 committed Jul 23, 2023
1 parent 0d3ebad commit 147f86c
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions docs/zjw/前端常见手写代码.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,32 @@ const debounceTask = debounce(task, 1000);
window.addEventListener('scroll', debounceTask);
```

首次立即执行

```js
function debounce(fn, delay) {
var timer = null;

return function () {
clearTimeout(timer);

const f = fn.bind(this, ...arguments);
const callNow = !timer;

if (callNow) {
f();
}
timer = setTimeout(() => {
timer = null;
// 非首次调用
if (!callNow) {
f();
}
}, delay);
};
}
```

## 节流

用于限制函数执行频率
Expand Down Expand Up @@ -77,6 +103,30 @@ function deepClone(obj) {
}
```

### stringify 方法实现

```js
function stringify(obj) {
if (obj === null || typeof obj !== 'object') {
// 数组 undefined 会被转换成 null
return obj === undefined || obj === null ? 'null' : obj;
}
if (Array.isArray(obj)) {
return '[' + json.map((m) => stringify(m)).join(',') + ']';
}

return (
'{' +
Object.keys(obj)
// 过滤值为 undefined 的 key
.filter((key) => obj[key] !== undefined)
.map((key) => `"${key}":${stringify(obj[key])}`)
.join(',') +
'}'
);
}
```

## 继承

### ES5 继承
Expand Down

0 comments on commit 147f86c

Please sign in to comment.