Originally my objective is when any property of the instances of class OnlyOneProp
is set,
i.e. obj.what_ever_property = "value"
,
at last it will only modify obj.the_only_prop
.
Behavior is like this:
var obj = new OnlyOneProp();
obj.what_ever_property = "value";
console.log(obj.only_property, obj.what_ever_property);
// expected output:
// >value undefined
Original Question: Is there a way to implement above behaviour?
edit:
With respect to the solution in the answer,
here are some follow up questions:
1) Is there any major flaw to the above code? (Like I had once mixed up receiver
and target
, which may cause infinite looping on the setter)
2) Would Proxy
hinder the performance a lot?
3) Is there any way to bypass the above proxy setter? (Like defineProperty()
or so)
4) It can also be an implementation of ReadOnlyObject (after removing the setting line in setter), but would there be a better implementation? (It's a bit out of topic but I also want to know, because I really want to remove the top Proxy which is just overriding the constructor)