Skip to content

Commit ff08903

Browse files
committed
feat: 完成手写call & apply & bind & 解析url的params
1 parent 35cf812 commit ff08903

File tree

5 files changed

+84
-0
lines changed

5 files changed

+84
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
- [x] 生成 x\*y 的数组 用 0 填充
99
- [x] 扁平化数组
1010
- [x] 下划线转驼峰
11+
- [x] 手写 call
12+
- [x] 手写 apply
13+
- [x] 手写 bind
14+
- [x] 手写 url 解析参数
1115

1216
## 排序算法
1317

面试常见的手写题/08-call.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Function.prototype.MyCall = function (thisArg, ...arg) {
2+
thisArg = thisArg !== null && thisArg !== undefined ? Object(thisArg) : window
3+
thisArg.fn = this
4+
const res = thisArg.fn(...arg)
5+
delete thisArg.fn
6+
return res
7+
}
8+
9+
function foo(a, b, c) {
10+
console.log(this.name, a, b, c)
11+
}
12+
13+
const obj = {
14+
name: 'coderwei',
15+
}
16+
17+
foo.MyCall(obj, 1, 2, 3)

面试常见的手写题/09-apply.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Function.prototype.MyApply = function (thisArg, arg) {
2+
thisArg = thisArg !== null && thisArg !== undefined ? Object(thisArg) : window
3+
thisArg.fn = this
4+
const res = thisArg.fn(...arg)
5+
delete thisArg.fn
6+
return res
7+
}
8+
9+
function foo(a, b, c) {
10+
console.log(this.name, a, b, c)
11+
}
12+
13+
const obj = {
14+
name: 'coderwei',
15+
}
16+
17+
foo.MyApply(obj, [1, 2, 3])

面试常见的手写题/10-bind.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Function.prototype.MyBind = function (thisArg, ...arg) {
2+
thisArg = thisArg !== null && thisArg !== undefined ? Object(thisArg) : window
3+
thisArg.fn = this
4+
// const res = thisArg.fn(...arg)
5+
return function (...arg2) {
6+
const res = thisArg.fn(...arg, ...arg2)
7+
return res
8+
}
9+
}
10+
11+
function foo(a, b, c) {
12+
console.log(this.name, a, b, c)
13+
return 1
14+
}
15+
16+
const obj = {
17+
name: 'coderwei',
18+
}
19+
20+
const bar = foo.MyBind(obj, 1)
21+
console.log(bar(2, 3))
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//考虑存在重复的query的情况 输出格式为: { name: 'koala', study: [ 'js', 'node', '123' ] }
2+
let urlStr1 = 'http://www.inode.club?name=koala&study=js&study=node&study=123'
3+
4+
function queryString2(request) {
5+
let str = request.split('?')[1]
6+
console.log(str)
7+
let obj = {}
8+
const params = str.split('&')
9+
params.forEach((item) => {
10+
const [key, value] = item.split('=')
11+
if (obj[key]) {
12+
if (Array.isArray(obj[key])) {
13+
obj[key].push(value)
14+
} else {
15+
obj[key] = [obj[key], value]
16+
}
17+
} else {
18+
obj[key] = value
19+
}
20+
})
21+
22+
console.log(obj)
23+
return obj
24+
}
25+
queryString2(urlStr1)

0 commit comments

Comments
 (0)