I am trying to solve a problem that has occurred to me recently. Let's say we would like and would know how to make javascript have dynamic getters and setters, more like php (__get, __set). But since javascript does not have a catch-all property, the only thing we could do is provide a list of possible keys and iteration to add getters and setters only for them, and I hope that no one else comes.
But the problem is far from solved. So the next approach that came to my mind was to use the nasty hack with try and catch , so at any time the name undefined in the object would use catch as a getter (at least) and then resume the code, itโs hard and perhaps a pointless thing. But from here came my second problem, for example:
console.log(g.someundefinedproperty);
the result would be a console.log call showing undefined without exception. And then it came to me: what if I use the original window.undefined getter and setter, because it should be called every time I mess up and miss a word or something else.
So i tried
Object.defineProperty(window, 'undefined', { get : function () {
But, unfortunately, the undefined property of the configurable : false window. Other proven hacks cloned the window object, except for the undefined property and the internal window . And on the new object, define a new undefined (please pay attention to the irony), and then window = mybetterwindow ;
Since this did not cause any problems, my hopes were high, but again the system failed me, because window cannot be overwritten by design. I assumed that it has its own getter, and it is restored based on the prototype found in window.prototype or even better than window.prototype (pay attention to uppercase).
As my last step in this experiment, I redefined undefined on this run prototype. Nothing has changed to no avail ... I tried to create new Window() , but window not a constructor, it does not work!
As my ideas run out, I am here writing this request for help. If you have ideas on how to solve the problem of dynamic getters and setters, ( the existencial problem of life, universe and everything else ), so that it doesnโt change in any way ... the way you use objects (and as a bonus, this no need to break a hole in fabric of time and space ) or syntax, I beg you to speak or be silent forever :).