Skip to content

Commit b10e0b8

Browse files
committed
手写new
1 parent ff3fe03 commit b10e0b8

File tree

7 files changed

+217
-0
lines changed

7 files changed

+217
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Document</title>
8+
</head>
9+
<body>
10+
<section>
11+
<style>
12+
.wrapper {
13+
height: 500px;
14+
background-color: red;
15+
}
16+
.left{
17+
height: 100%;
18+
width: 200px;
19+
background-color: blue;
20+
}
21+
</style>
22+
<div class="wrapper">
23+
<div class="left"></div>
24+
<div class="right"></div>
25+
</div>
26+
</section>
27+
</body>
28+
</html>

笔试题/js基础/test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
var a = 2;
3+
a = 5;
4+
window.a = 3;
5+
6+
function test() {
7+
let a = 2;
8+
this.a = 10
9+
setTimeout(function name(params) {
10+
console.log(a)
11+
},20)
12+
setTimeout(function name(params) {
13+
console.log(this.a)
14+
},10)
15+
setTimeout((params) => {
16+
console.log(this.a)
17+
},10)
18+
}
19+
20+
test()

笔试题/js基础/基础.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
## 变量
2+
js的数据类型 对应typeof的返回值
3+
值类型: Boolean Number String bigint Syboml undefined
4+
引用类型: object null
5+
6+
网络
7+
url到页面呈现
8+
https原理
9+
ca证书,私钥在服务端,公钥+证书传输到客户端,
10+
客户端验证证书信息,证书信息不符弹出警告
11+
客户端拿到公钥加密随机key发送到服务端
12+
服务端拿到解密的key,对称加密传输的数据
13+
http缓存
14+
cache-contro
15+
16+
17+
html
18+
垂直居中定位
19+
position
20+
flex
21+
盒模型
22+
border-box
23+
conent-box
24+
BFC
25+
用来清楚浮动,。。。
26+
重排,重绘
27+
28+
es6
29+
set map ...
30+
promise 3个状态,padding resolve reject
31+
红任务微任务
32+
事件队列
33+
继承 call 共享原型,圣杯

笔试题/vue/refact.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Dep module
2+
class Dep {
3+
static stack = []
4+
static target = null
5+
deps = null
6+
7+
constructor() {
8+
this.deps = new Set()
9+
}
10+
11+
depend() {
12+
if (Dep.target) {
13+
this.deps.add(Dep.target)
14+
}
15+
}
16+
17+
notify() {
18+
this.deps.forEach(w => w.update())
19+
}
20+
21+
static pushTarget(t) {
22+
if (this.target) {
23+
this.stack.push(this.target)
24+
}
25+
this.target = t
26+
}
27+
28+
static popTarget() {
29+
this.target = this.stack.pop()
30+
}
31+
}
32+
33+
// reactive
34+
function reactive(o) {
35+
if (o && typeof o === 'object') {
36+
Object.keys(o).forEach(k => {
37+
defineReactive(o, k, o[k])
38+
})
39+
}
40+
return o
41+
}
42+
43+
function defineReactive(obj, k, val) {
44+
let dep = new Dep()
45+
Object.defineProperty(obj, k, {
46+
get() {
47+
dep.depend()
48+
return val
49+
},
50+
set(newVal) {
51+
val = newVal
52+
dep.notify()
53+
}
54+
})
55+
if (val && typeof val === 'object') {
56+
reactive(val)
57+
}
58+
}
59+
60+
// watcher
61+
class Watcher {
62+
constructor(effect) {
63+
this.effect = effect
64+
this.update()
65+
}
66+
update() {
67+
Dep.pushTarget(this)
68+
this.value = this.effect()
69+
Dep.popTarget()
70+
return this.value
71+
}
72+
}
73+
74+
// 测试代码
75+
const data = reactive({
76+
msg: 'aaa'
77+
})
78+
79+
new Watcher(() => {
80+
console.log('===> effect', data.msg);
81+
})
82+
83+
setTimeout(() => {
84+
data.msg = 'hello'
85+
}, 1000)

笔试题/手写api/flat.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function recursionFlat(ary = []) {
2+
const res = []
3+
ary.forEach(item => {
4+
if (Array.isArray(item)) {
5+
res.push(...recursionFlat(item))
6+
} else {
7+
res.push(item)
8+
}
9+
})
10+
return res
11+
}
12+
// 方案 2
13+
function reduceFlat(ary = []) {
14+
return ary.reduce((res, item) => res.concat(Array.isArray(item) ? reduceFlat(item) : item), [])
15+
}
16+
17+
// 测试
18+
const source = [1, 2, [3, 4, [5, 6]], '7']
19+
console.log(recursionFlat(source))
20+
console.log(reduceFlat(source))

笔试题/手写api/instensof.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function instanceOf(left,right) {
2+
let proto = left.__proto__;
3+
let prototype = right.prototype
4+
while(true) {
5+
if(proto === null) return false
6+
if(proto === prototype) return true
7+
proto = proto.__proto__;
8+
}
9+
}

笔试题/手写api/new.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function myNew(Func, ...args) {
2+
const instance = {};
3+
if (Func.prototype) {
4+
// instance.__proto__ = Func.prototype // 不推荐直接操作隐式原型
5+
Object.setPrototypeOf(instance, Func.prototype)
6+
}
7+
const res = Func.call(instance, ...args)
8+
if (typeof res === "function" || (typeof res === "object" && res !== null)) {
9+
return res
10+
}
11+
return instance
12+
}
13+
// 测试
14+
function Person(name) {
15+
this.name = name
16+
}
17+
Person.prototype.sayName = function () {
18+
console.log(`My name is ${this.name}`)
19+
}
20+
const me = myNew(Person, 'WangJie')
21+
me.sayName()
22+
console.log(me)

0 commit comments

Comments
 (0)