Edit in JSFiddle

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

   return {
     x: x,
     getX: getX,
     setX: setX
   };
}

var o = myObjCreator();

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/>");