atlas-bind-methods
Binds an instance's prototype methods to itself.
Why
Suppose you have a class which uses instance variables in its methods:
// Animal.jsmoduleexports = { thissound = sound; } { console }
Normally, the following won't work:
const Animal = ;const cat = "meow!";const rawr = catrawr;; // TypeError: Cannot read property 'sound' of undefined
It fails because the reference loses the method's context, but you already knew that! The fix is pretty simple -- just bind the function to the instance:
...const rawr = catrawr // meow!
But, this is kind of annoying when I have to do this for many methods. Enter atlas-bind-methods
...
Example
This package provides a concise function which lets you do the binding in one line:
const Animal = ;const bind = ;const cat = ;const rawr = catrawr; // meow!
Or, if you prefer the side effect, that's fine too:
...const cat = "meow!";;const rawr = catrawr; // meow!
Caveats
This doesn't support subclassing, so it would only pre-bind the prototype methods of the direct constructor which is used to instantiate the object.
Install
npm install --save atlas-bind-methods