O'Reilly logo
  • Ahmed Elgendy thinks this is interesting:

So why did we create and call the start method instead of just changing the started property directly? Using a method to change a property is another example of encapsulation whereby we can often improve the maintainability and extensibility of code by letting an object worry about how it gets things done. It’s better to have a start method that knows how to start the car than for you to have to know “to start the car we need to take the started variable and set it to true.”

Now you may still be saying “What’s the big deal? Why not just set the property to true to start the car?!&...


Cover of Head First JavaScript Programming


What is object encapsulation?