Another solution, which requires some initial setup, but pays off with its invincible light, literally in a word, uses Decorators methods for JIT-bind methods through getters.
I created a repository on GitHub to demonstrate the implementation of this idea (it's a bit long to fit in with the answer from its 40 line of code, including comments), which you would use the same way:
class DemonstrateScopingProblems { private status = "blah"; @bound public run() { alert(this.status); } }
I have never seen this anywhere, but it works flawlessly. In addition, there is no noticeable flaw in this approach: the implementation of this decorator, including some type checking for safety at runtime, is trivial and simple, and when the method is invoked with zero overhead, the value is almost zero.
The essential part is the definition of the following getter in the prototype of the class, which is executed immediately before the first call:
get: function () {
Full source
The idea can also be taken one step further by doing it in the class decorator instead, iterating over the methods and defining the property descriptor above for each of them in a single pass.
John Weisz Oct 06 '16 at 21:11 2016-10-06 21:11
source share