-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhomework.js
68 lines (52 loc) · 1.2 KB
/
homework.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class A {
constructor() {
this.nameA = 'a'
}
validateA() {
console.log("A")
}
}
class B extends A {
constructor() {
super()
this.nameB = 'b'
}
validateB() {
console.log("B")
}
}
class C extends B {
constructor() {
super()
this.nameC = 'c'
}
validateC() {
console.log("C")
}
}
function findMembers(instance, fieldPrefix, funcPrefix) {
// 递归函数
function _find(instance) {
//基线条件(跳出递归)
console.log(instance.__proto__, 'instance.__proto__')
if (instance.__proto__ === null)
return []
let names = Reflect.ownKeys(instance)
console.log(names, 'names')
names = names.filter((name)=>{
// 过滤掉不满足条件的属性或方法名
return _shouldKeep(name)
})
return [...names, ..._find(instance.__proto__)]
}
function _shouldKeep(value) {
if (value.startsWith(fieldPrefix) || value.startsWith(funcPrefix))
return true
}
return _find(instance)
}
var c = new C()
// 编写一个函数findMembers
const members = findMembers(c, 'name', 'validate')
console.log(Reflect.ownKeys(c), members)
// 原型链 查找