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());

1 Answer 1

1

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

obj.get().id = 33;
Sign up to request clarification or add additional context in comments.

2 Comments

Makes sense. But is it safe? Can it be editable from 3rd party tools, or hackers?
here safety depends upon the access, you will provide to obj. Read this to get insight of access modifeir. stackoverflow.com/questions/1958216/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.