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)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
which part of quick and dirty wasn't clear? it's a proof of concept ;-)