JSFiddle - React, Tailwind, and code Playground

JavaScript

function myObjCreator2() {
        var x = 1;

        function getX() { return x; }
        function setX(val) { x = val; }

        var ret = {
            x: x,
            getX: getX,
            setX: setX
        };
        x = 5;
        return ret;
    }

var o = myObjCreator2();

document.write(o.x + "<BR/>");
document.write(o.getX() + "<BR/>");

o.x = 2;

document.write(o.x + "<BR/>");
document.write(o.getX() + "<BR/>");

o.setX(3);

document.write(o.x + "<BR/>");
document.write(o.getX() + "<BR/>");