4 ms·
In fact in most cases dynamic subclassing is enough for such things. In JavaScript that's achieved by changing __proto__ reference on an object. So "obj instan
by c-smile 4y ago
In fact in most cases dynamic subclassing is enough for such things.
In JavaScript that's achieved by changing __proto__ reference on an object.
So "obj instanceof A" becomes "obj instanceof B".
As of persistence case I've solved it on JavaScript internal implementation level.
In Sciter there is built-in JSON-ish data persistence module - close to Mongo-DB on feature set (modulo sharding).
Storage loads objects as half-backed proxies that contain only db references.
Only when code tries to access props/methods of the loaded object it gets fetched from disk, its __proto__ is set to particular class, etc.
More on this architecture: https://gitlab.com/sciter-engine/sciter-js-sdk/-/blob/main/docs/md/storage/architecture.md https://gitlab.com/sciter-engine/sciter-js-sdk/-/blob/main/d...
By the way, dynamic subclassing is not a prerogative of only dynamic languages, here is how I did that in C++:
https://stackoverflow.com/questions/21212379/changing-vtbl-of-existing-object-on-the-fly-dynamic-subclassing https://stackoverflow.com/questions/21212379/changing-vtbl-o...
Patched QuickJS with storage support is here: https://gitlab.com/c-smile/quickjspp https://gitlab.com/c-smile/quickjspp - it uses DyBase of Konstantin Knizhnik as a storage.
- kazinator 4y agoNot really, because the goal is that obj is replaced by a different instance, not to change its type. Become can swap two objects that are exactly of the same type, where the prototype change would be a null operation, achieving nothing.
- c-smile 4y ago"obj is replaced by a different instance" Could you provide real life scenario when you will need that? I mean to change all references to the object in the heap at runtime ...
- pjmlp 4y agoLive code editing on Smalltalk debugger, after changes on the class browser, redoing statement that caused the break into debugger, and have all live instances on the image updated.