-
Notifications
You must be signed in to change notification settings - Fork 181
/
51.isCyclic.js
70 lines (55 loc) · 1.25 KB
/
51.isCyclic.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
69
70
const isCyclic = (obj) => {
let stackSet = new Set()
let detected = false
const detect = (obj) => {
if (obj && typeof obj != 'object') {
return
}
if (stackSet.has(obj)) {
return detected = true
}
stackSet.add(obj)
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
detect(obj[key])
}
}
stackSet.delete(obj)
}
detect(obj)
return detected
}
// 对象之间相互引用
let obj1 = { name: '前端胖头鱼1' }
let obj2 = { name: '前端胖头鱼2' }
// 对象1的属性引用了对象2
obj1.obj = obj2
// 对象2的属性引用了对象1
obj2.obj = obj1
let obj = { name: '前端胖头鱼1' }
// 对象的属性引用了对象本身
obj.child = obj
let obj3 = {
name: '前端胖头鱼',
child: {}
}
obj3.child.obj = obj3.child
let tempObj = {
name: '前端胖头鱼'
}
let obj4 = {
obj1: tempObj,
obj2: tempObj
}
console.log(isCyclic(obj1))
console.log(isCyclic(obj2))
console.log(isCyclic(obj))
console.log(isCyclic(obj3))
console.log(isCyclic(obj4))
console.log(isCyclic(1))
console.log(isCyclic('前端胖头鱼'))
console.log(isCyclic(false))
console.log(isCyclic(null))
console.log(isCyclic(undefined))
console.log(isCyclic([]))
console.log(isCyclic(Symbol('前端胖头鱼')))