JSFiddle - React, Tailwind, and code Playground

by 2Yootz

JavaScript

var color = 'black';

function Box() {
  var self = this;
  self.color = 'purple';
    
  // private constructor 
  var __construct = function() {
    self.changeToRed();
    alert(self.color); // should be red
    self.color = 'green';
  };

  // public property
  self.color = '';

  // getter
  self.getColor = function() {
    return self.color;
  };

  // setter
  self.setColor = function(color) {
    self.color = color;
  };

  // testing callable method from contructor
  self.changeToRed = function() {
    self.color = 'red';
  };

  __construct();

}

var b = new Box();

alert(b.getColor()); // should be green

b.setColor('orange');

alert(b.getColor()); // should be orange

alert(color); // should be black