JavaScript

Last Updated: 2/2/2023

Abstraction

  • Abstraction means we should hide the details and complexity and show or expose only the essentials.
  • Think of a DVD player. A DVD player has a complex logic board on the inside, but only a few buttons on the outside that you interact with. So what we have on the inside is the implementation detail, and what we have on the outside, is the public interface of a DVD player.
  • We want our objects to look like this DVD player. We want to hide all the details, all the unnecessary complexity on the outside, and expose only a few members, or a few buttons on the outside.
  • If everything in the objects, is public and accessible from the outside. And this will bring a number of issues. One issue is that every time we change the implementation of that object, you have to modify many different places in your code.
function Circle(radius) {
    // Private properties and methods
    let defaultLocation = {x: 0, y:0};
    let computeOptimumLocation = function() {

    }

    this.radius = radius;
    this.draw = function() {

    }

    // Public Properties getters and setters
    Object.defineProperty(this, "defaultLocation", {
        get: function() {
            return defaultLocation;
        },
        set: function(value) {
            defaultLocation = value;
        }
    })
}