Path: csiph.com!weretis.net!feeder4.news.weretis.net!news.mb-net.net!open-news-network.org!.POSTED.39.234.197.178.dynamic.wless.lssmb00p-cgnat.res.cust.swisscom.ch!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: Recording changes to an object Date: Fri, 22 Jun 2018 02:55:22 +0200 Organization: PointedEars Software (PES) Lines: 61 Message-ID: <1643274.5VXe1CXSyS@PointedEars.de> References: <1620852.VI2in3QvqR@PointedEars.de> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit Injection-Info: gwaiyur.mb-net.net; posting-host="39.234.197.178.dynamic.wless.lssmb00p-cgnat.res.cust.swisscom.ch:178.197.234.39"; logging-data="32561"; mail-complaints-to="abuse@open-news-network.org" User-Agent: KNode/4.14.2 X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. On 22/06/2018 5:03 AM, Thomas 'PointedEars' Lahn wrote: >> Property changes should only be possible via setter. You can either make >> the actual properties private and have a getter that returns their >> actual value – > > [nice code clipped] > > Great thanks, it looks an implementation of the same idea as Julio's. > And it looks like it's something I can implement without too much pain. > I just need to go through the dozens of places that the object is > currently changed and implement the getter/setter. Depending on what you mean by that, you might not be aware that even if you cannot use Proxy you may not have to implement the getter/setter in multiple places: /* code that defines o */ function proxify (target, propertyNames) { propertyNames.forEach(function (name) { var _p = this[name]; /* Object.defineProperty(this, name, { "get": function () { return _p; }, "set": function (value) { // … _p = value; // … } }); }, target); } proxify(o, [propertyName1, propertyName2]); /* code that uses o */ Note that by having a top-level setter you can make sure that sub-level properties also use a setter: just convert the assigned value, if it is a reference to an object, to a reference to a object whose relevant properties have setters. You can do the same with Proxy, but there also for yet unknown properties; also, only with Proxy you can trap deletion of properties. Note also that getters and setters have been supported in some implementation before Object.defineProperty(), with Object.prototype.__defineGetter__() and Object.prototype.__defineSetter__(), which you can use as a fallback in Object.defineProperty() polyfill (as I do in JSX:object.js). -- PointedEars FAQ: | | Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.