js Object.freeze All In One
const obj = {a: 1, b: 2}
// {a: 1, b: 2}
const fobj = Object.freeze(obj);
//{a: 1, b: 2}
fobj.a;
//1
fobj.a = 2;
// 2 ❌ 無法修改屬性的值
fobj;
// {a: 1, b: 2}
fobj.c = 3;
// 3 ❌ 無法添加新的屬性
fobj;
// {a: 1, b: 2}
Object.freeze()
方法用於凍結
一個對象
一個對象被凍結後就不能被修改了,凍結後不能向該對象添加
新的屬性,不能
刪除該對象的
已有屬性,不能
修改該對象已有屬性的可枚舉性、可配置性、可寫性,屬性的
值; 一個對象被凍結後,該對象的
原型`也不能被修改;
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze
Singleton Pattern
https://www.patterns.dev/posts/singleton-pattern/
https://github.com/xgqfrms/Design-Patterns
refs
https://stackoverflow.com/questions/33124058/object-freeze-vs-const
https://juejin.cn/post/6884384986061864967
©xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 發佈文章使用:只允許註冊用戶纔可以訪問!
原創文章,版權所有©️xgqfrms, 禁止轉載 🈲️,侵權必究⚠️!