# `this` is undefined. Shouldn't it be a reference to my plugin? #obsidian/api/faq --- Mozilla has a great explanation for what [`this`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this) means in their documentation. I recommend reading that first. Still confused? Here's my oversimplified explanation: `this` doesn't necessarily refer to the current object instance. Its meaning changes based on the context that it's called. So if you are passing a function (i.e. `this.myCallbackFunction`) as a callback to be called outside the current Plugin context, you'll need to use [`bind`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind). ## Rule of thumb If you're passing a method as a callback to an event listener (e.g. `vault.on('file-open', this.myFileOpenHandler)`), you'll need to `bind`. ### Bind ```ts vault.on('file-open', this.myFileOpenHandler.bind(this)); ``` ### Arrow Function If you use ES6 Arrow functions, you don't need to use `bind`. That happens automatically. ```ts vault.on('file-open', () => this.myFileOpenHandler()); ``` This also works fine. `bind` is usually preferred since there's less indirection. Here you are actually passing a function that calls another function. It's slightly more expensive. ## Note about function identity When using `bind()` or wrapping your function in an Arrow function, that function is not the same reference as your original function. ```ts this.myFunction !== this.myFunction.bind(this); // Bind returns a new function this.myFunction !== () => this.myFunction(); // The arrow function is an anonymous function that happens to call your function. ``` [[#When do I need to worry about this|If you need a reference to that actual function]] that's called by the callback, a common pattern is to reassign the function to it's _bound_ equivalent: ```ts this.myFunction = this.myFunction.bind(this); ``` ### When do I need to worry about this? One example is when you call [`Events.off()`](https://github.com/obsidianmd/obsidian-api/blob/81ab85ade4552c9116c1e10d009127e62019c923/obsidian.d.ts#L672) in the Obsidian API. `off` expects you to pass the event name + the callback reference.