autobind-av
v1.0.2
Published
A function that automatically binds your methods to the class instance.
Readme
autobind-av
A tiny utility to automatically bind class methods to their instance, allowing you to write clean, overridable methods without using arrow functions.
✨ Why
When you use arrow functions inside classes, they get bound to the instance automatically — which is great — but they also lose some of their behavior as proper class methods:
- You can't override them from subclasses.
- They don't live on the class prototype, which makes them harder to mock/test/extend.
- They're recreated for every instance, slightly increasing memory usage.
- This library allows you to write standard class methods that:
- Can be overridden.
- Retain their prototype behavior.
- Still work correctly when passed as callbacks (this stays bound).
⚙️ Usage
Copy And Paste (good old solution)
You can copy the code directly from here and paste in your ts/js file.
Remove any types in the js file)
Install as package
npm install autobind-avimport { autobind } from "autobind-av";
class MyClass {
private label = "Bound";
constructor() {
autobind(this); // 👈 This will automatically bind methods
}
myMethod() {
console.log("Hello from", this.label);
}
myArrow = () => {
console.log("This is already bound");
}
}
const instance = new MyClass();
setTimeout(instance.myMethod, 1000); // ✅ prints "Hello from Bound"Parameters
- autobind(instance: object, methodsToExclude?: string[]): void;
instance: Your class instance (this).methodsToExclude: Optional array of method names to skip (e.g. ['onDestroy']).
