简体   繁体   中英

How to set/ edit a property of an object by using setter

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

尝试此操作,您必须在更改其值之前获取该对象:

obj.get().id = 33;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM