JSFiddle - React, Tailwind, and code Playground

by Joshua Dwire

JavaScript

function callback(obj, key, val) {
    console.log(key + " was set to " + val + " on ", obj);
}

var obj = {};
(function () {
    var a = null;
    obj.setA = function (value) {
        a = value;
        callback(obj, 'a', value); // triggers callback function
    }
    obj.getA = function () {
        return a;
    }
})()

console.log("a is " + obj.getA()); // a is null
obj.setA("something"); // Set a to something
console.log("a is now " + obj.getA()); // a is now something
obj.a = "something else"; // Set obj.a to something else to show how a is only accessible through setA
console.log("a is still " + obj.getA()); // a is still something