0

According to my research, I decided using getter/setter for my global variables(please correct me if I am wrong). I can set and get the private variable, but how can I edit value of a property in the object?

Since I no longer can use obj.id = "33", I tried obj.id.set("33") which makes no sense. How can I edit a value in the object?

var x = {"id":"93","customId":"a1a8d3c5af2d4807879e5fc6721d65ad","accountNumber":null};

var obj = (function() {
 var holder = "";
 return {
  get: function() {
   return holder;
  },
  set: function(val) {
   holder = val;
  }
 };
})();
console.log("before setting: ", obj.get());
obj.set(x);
console.log("after setting" ,obj.get());
//obj.id = "33";
// or
// obj.id.set("33");
//console.log("new id: ", obj.get());
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
Malena T
  • 341
  • 2
  • 7
  • 22

1 Answers1

1

try this, you must get that object before changing its value:

obj.get().id = 33;
Sachin Gupta
  • 7,805
  • 4
  • 30
  • 45
  • Makes sense. But is it safe? Can it be editable from 3rd party tools, or hackers? – Malena T Aug 08 '17 at 08:28
  • here safety depends upon the access, you will provide to `obj`. Read this to get insight of access modifeir. https://stackoverflow.com/questions/1958216/how-do-i-mimic-access-modifiers-in-javascript-with-the-prototype-library – Sachin Gupta Aug 08 '17 at 08:33