项目作者: fsvieira

项目描述 :
Javascript Object Factory for unique reference shared object creation.
高级语言: JavaScript
项目地址: git://github.com/fsvieira/identicobjects.git
创建时间: 2018-09-09T19:19:11Z
项目社区:https://github.com/fsvieira/identicobjects

开源协议:MIT License

下载


IdenticObjects

Javascript Object Factory for unique reference shared object creation.

It creates unique objects references for same objects, since objects
share references of unique objects inner equal objects will also share
same references, even if there father are not equal.

All registered objects are read-only and therefor they can not be changed.

install

npm install identicobjects --save

how to use

  1. const IdenticObjects = require("identicobjects");
  2. const objects = new IdenticObjects();
  3. const a = objects.get({
  4. a: {
  5. b: {
  6. c: [{}]
  7. }
  8. }
  9. });
  10. const b = objects.get({
  11. a: {
  12. b: {
  13. c: [{}]
  14. }
  15. },
  16. b: []
  17. });
  18. const c = objects.get(1);
  19. console.log(c); // print 1
  20. // a and b, deep c field is equal,
  21. console.log(a, b, a.a.b.c[0] === b.a.b.c[0]); // a.a.b.c[0] === b.a.b.c[0] = true
  22. // a and b are not equal,
  23. console.log(a, b, a === b); // a === b = false
  24. a.d = 1;
  25. // a is unchanged since a is read-only.
  26. console.log(JSON.stringify(a));

Use cases

  • Comparing objects created on the fly or coming from different sources,
  • Use objects as unique key on a Map, or as a unique Set value.