Last active
April 4, 2021 00:33
-
-
Save WebReflection/ee4695c107339e039878b02afb90dc0d to your computer and use it in GitHub Desktop.
Using `this.super()` and `this.super.method()` via any kind of object or prototype (proof of concept)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var withSuperContext = (function (Object) { | |
| //! (c) Andrea Giammarchi | |
| var | |
| defineProperty = Object.defineProperty, | |
| getPrototypeOf = Object.getPrototypeOf, | |
| setPrototypeOf = Object.setPrototypeOf || | |
| function (o, p) { o.__proto__ = p; }, | |
| handler = { | |
| get: function get(target, property, receiver) { | |
| return function () { | |
| var | |
| self = target.self, | |
| proto = getPrototypeOf(self), | |
| method = self[property], | |
| parent = proto, | |
| result | |
| ; | |
| do { | |
| parent = getPrototypeOf(parent); | |
| } while (method === parent[property]); | |
| setPrototypeOf(self, parent); | |
| try { | |
| result = parent[property].apply(self, arguments); | |
| setPrototypeOf(self, proto); | |
| } catch(e) { | |
| setPrototypeOf(self, proto); | |
| throw e; | |
| } | |
| return result; | |
| }; | |
| } | |
| } | |
| ; | |
| return function withSuperContext(object) { | |
| defineProperty( | |
| typeof object === 'function' ? | |
| object.prototype : object, | |
| 'super', | |
| { | |
| get: function get() { | |
| var | |
| self = this, | |
| parent = function () { | |
| return proxy.constructor.apply(self, arguments); | |
| }, | |
| proxy = new Proxy(parent, handler) | |
| ; | |
| parent.self = self; | |
| return proxy; | |
| } | |
| } | |
| ); | |
| return object; | |
| }; | |
| }(Object)); |
Also you will totally want to wrap setPrototypeOf(self, proto); in a finally clause, or otherwise the prototype chain will explode in your face when one of the method throws an exception.
Author
which part of quick and dirty wasn't clear? it's a proof of concept ;-)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This does not seem to work if
Cdoes not define themethod: WhenB.prototype.methoddoes callsuper, theselfwill refer to the C instance,prototoC.prototype, and you'll get an infinite recursion whenBsmethodcalls itself.Instead of assuming that the
superwas gotten in a method on the current direct prototype ofthis/self, you rather should pass theobject/object.prototypeinto the proxy to be used as theproto.