0

I'm a bit confused

While it's always mentioned that encapsulation allows us to not expose variables to outer world and we can have setter and getter functions in order to have access, does it really help with avoiding to directly set the value of an object property ?

I mean what's the idea in having a setter and a getter function if one can easily change the property with object.name=***** this kind of syntax?

What is the real idea behind encapsulation ?

Thanks ?

gurbajani
  • 111
  • 9
  • 1
    You have to distinguish between the abstract concept of *encapsulation* and the practical implementation. The concept is to hide implementation details. As for the practical implementation, JS until recently didn't have private properties, but you could emulate them in a variety of ways (see duplicate above). In practice, a simple naming convention like `object._name` is enough, as it signals to you and others that this property should not be accessed from outside, and as long as nobody does, you have your encapsulation concept implemented. – deceze May 25 '22 at 11:43

0 Answers0