Javascript setter for object value

I created a "set" function to which it will run when the value of my "myObject.user" object changes. Therefore, when I change the value, I get a warning. but when I update the value inside the object "myObject.newuser [0] .id", this set does not call. How to change the code so that when changing any internal value, the global set works. those. for this code I should get two warnings.

var myObject = {}; myObject.user; Object.defineProperty(myObject, 'newuser', { get: function() { return this.user;}, set: function(value) { alert('Updated');this.user=value;} }); myObject.newuser = [{"id":"user1"}]; myObject.newuser[0].id="user2"; 
+5
source share
1 answer

Well, you need to change the property myObject.newuser[0].id to myObject.user[0].id , after which it will set the new value to the existing value of the user.

Since you are changing the previously added value with the new value.

For more information about the customizer, see the MDN link .

0
source

Source: https://habr.com/ru/post/1209826/


All Articles