# 不可修改对象
const { object } = require('assert-plus')
function createConst(obj) {
const keys = Object.getOwnPropertyNames(obj)
keys.forEach(key => {
const prop = obj[key]
if (typeof prop === 'object' && prop !== null) {
obj[key] = createConst(prop)
}
})
Object.freeze(obj)
return new Proxy(obj, {
deleteProperty: function (target, key) {
throw new Error('不能删除' + key)
},
get: function (target, key) {
console.log(target, key)
if (!target.hasOwnProperty(key)) {
throw new Error(key + '不存在')
} else {
return target[key]
}
},
set: function (target, key) {
console.log(key)
throw new Error('不能修改'+key)
}
})
}
const settings = {
appName:"fan",
info: {p1:200,p2:300 }
};
const o = createConst(settings)
// settings.appName = 1 ;
// o.other = "abc";
// console.log(o.a)
// o.info.p1 = 100;
delete o.info.p1
console.info(o)
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
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