hide object property from all objects except one in javascript

Say i have the code:

function Obj(){
  var _x = 5;
  this.getX = function(){
    return _x;
  };
}

var obj = new Obj();

function God(){
  var x = obj.getX(); // i want 5 here
}

function Human(){
  var x = obj.getX(); // i want error/undefined here
}

var x = obj.getX(); // i want error/undefined here too

How can I hide the getX property from all objects except one?


Source: oop

Leave a Reply