-
-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/azl397985856/fe-interview
- Loading branch information
Showing
7 changed files
with
488 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
## 每日一题 - 删除文件是否需要对该文件具有写权限,为什么? | ||
|
||
### 信息卡片 | ||
|
||
- 时间:2019-11-14 | ||
- tag:`OS` | ||
|
||
### 题目描述 | ||
|
||
删除文件是否需要对该文件具有写权限,为什么? | ||
|
||
### 参考答案 | ||
|
||
删除文件不需要该文件的写权限,需要文件所在目录的写权限以及执行权限。 | ||
|
||
因为删除文件修改的该文件父级即其所在目录的内容,所以需要目录的写权限。 | ||
同时,删除文件先要进入到目录,进入是目录的一个操作,所以需要该目录的执行操作。 | ||
|
||
##### bash 验证 | ||
|
||
```bash | ||
mkdir a && touch a/b #新建a目录,a下有b文件 | ||
chmod -w a && rm a/b #去掉a的写权限,尝试去删除a/b,报 rm: a/b: Permission denied,说明删除文件需要文件所在目录有写权限 | ||
chmod +w a && chmod -w a/b && rm a/b #恢复a写权限,去掉b写权限,尝试去删除b, 删除成功,说明删除文件不需要写权限 | ||
touch b && chmod -x a && rm a/b #去掉a的执行权限 报rm: a/b: Permission denied,说明删除文件需要目录的执行权限 | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# 每日一题 - 2019-11-25 - 写一个debounce的装饰器 | ||
|
||
### 信息卡片 | ||
|
||
- 时间:2019-11-25 | ||
- tag:`编程题` | ||
|
||
### 问题描述 | ||
实现一个debounce装饰器 | ||
|
||
### 参考实现 | ||
|
||
##### 代码实现 | ||
```typescript | ||
/** | ||
* 装饰器的debounce | ||
* @param delay | ||
*/ | ||
export function debounce(delay: number): Function { | ||
return ( | ||
target: Function, | ||
propertyKey: string, | ||
propertyDesciptor: PropertyDescriptor | ||
) => { | ||
const method = propertyDesciptor.value; | ||
let timer = null; | ||
propertyDesciptor.value = (...args) => { | ||
if (timer) { | ||
clearTimeout(timer); | ||
timer = null; | ||
} | ||
timer = setTimeout(() => method(...args), delay); | ||
}; | ||
return propertyDesciptor; | ||
}; | ||
} | ||
``` | ||
|
||
##### 单元测试 | ||
|
||
``` typescript | ||
import { debounce } from './index'; | ||
|
||
jest.useFakeTimers(); | ||
|
||
let a: any; | ||
let mockFunc: jest.Mock; | ||
beforeEach(() => { | ||
mockFunc = jest.fn(); | ||
class Test { | ||
@debounce(1000) | ||
sayHi() { | ||
mockFunc(); | ||
} | ||
} | ||
a = new Test(); | ||
}); | ||
|
||
describe('debounce:', () => { | ||
test('debounced function should be called after the delay time', () => { | ||
a.sayHi(); | ||
expect(mockFunc).toHaveBeenCalledTimes(0); | ||
jest.advanceTimersByTime(1000); | ||
expect(mockFunc).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test('debounced function should not be called before the delay time', () => { | ||
a.sayHi(); | ||
expect(mockFunc).toHaveBeenCalledTimes(0); | ||
let count = 100; | ||
while (count--) { | ||
a.sayHi(); | ||
} | ||
expect(mockFunc).toHaveBeenCalledTimes(0); | ||
|
||
count = 100; | ||
while (count--) { | ||
jest.advanceTimersByTime(999); | ||
a.sayHi(); | ||
} | ||
expect(mockFunc).toHaveBeenCalledTimes(0); | ||
}); | ||
}); | ||
``` | ||
|
||
执行结果 | ||
![](assets/2019-11-25 - 写一个debounce的装饰器-unit-test.png) | ||
|
||
|
||
### 扩展 | ||
|
||
- 写一个throttle的装饰器 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.