Patrick Roza, thank your for the replay, in my example I'm talking in the context of the current JS implementation not the next JS or TS, for sure typescript solved the issue for now with private properties/methods.
I use encapsulation mainly to avoid shared mutating state issue and avoid an example like the following:
deleteobj.someProp;
I can freeze or use Object.defineProperty to achieve it, the later is more waste of effort.
I don't think that using WeakMaps has a big performance impact compared to closures.
I see, in TS trying to delete obj.someProp where someProp is marked readonly will be prevented by compiler.
yea I just noticed that in vanilla JS you are kind of left to the harder options like closures or Weakmap.
At the same time, JSDoc may be helpful too, as said communication is the most important part imo, achievable by:
naming: e.g _myprivate, _MyClass_myprivate etc
documentation: e.g /** @private */
tooling support for said options (lint/doc/editor)
Editor support is of course very helpful, and in that, I think TypeScript as a supported standard is perhaps very helpful.
Patrick Roza, thank your for the replay, in my example I'm talking in the context of the current JS implementation not the next JS or TS, for sure typescript solved the issue for now with private properties/methods.
I use encapsulation mainly to avoid shared mutating state issue and avoid an example like the following:
I can
freeze
or useObject.defineProperty
to achieve it, the later is more waste of effort.I don't think that using
WeakMap
s has a big performance impact compared to closures.I see, in TS trying to
delete obj.someProp
wheresomeProp
is markedreadonly
will be prevented by compiler.yea I just noticed that in vanilla JS you are kind of left to the harder options like closures or Weakmap.
At the same time, JSDoc may be helpful too, as said communication is the most important part imo, achievable by:
_myprivate
,_MyClass_myprivate
etc/** @private */
Editor support is of course very helpful, and in that, I think TypeScript as a supported standard is perhaps very helpful.
I agree, I use the example above for domain modeling with JS on nodejs, not in every part of my applications.
As you said, communication is important so I started switching to typescript.
+1 for the JSDoc.