Skip to content

Commit 58aab91

Browse files
committed
feat: 完成手写Object.freeze函数
1 parent 5017c64 commit 58aab91

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const syn = Symbol(1)
2+
const obj = {
3+
name: 'coderwei',
4+
age: 13,
5+
friend: {
6+
name: 'test1',
7+
},
8+
[syn]: 123,
9+
}
10+
11+
Object.prototype.dadad = '123'
12+
13+
Object._freeze = function (obj) {
14+
const ObjKey = Object.getOwnPropertyNames(obj)
15+
const SymbolKey = Object.getOwnPropertySymbols(obj)
16+
const keys = [...ObjKey, ...SymbolKey]
17+
18+
for (const key of keys) {
19+
Object.defineProperty(obj, key, {
20+
writable: false,
21+
configurable: false,
22+
})
23+
}
24+
}
25+
26+
Object._freeze(obj)
27+
28+
function hasOwnProtoType(obj, key) {
29+
return Object.prototype.hasOwnProperty.call(obj, key)
30+
}
31+
obj[syn] = '被修改了'
32+
obj.name = '被修改了'
33+
obj.age = '被修改了'
34+
obj.friend.name = '被修改了'
35+
console.log(obj)

0 commit comments

Comments
 (0)