Lesley's Digital Garden

Home

❯

Notes

❯

JavaScript bind()

JavaScript bind()

Mar 14, 2024

  • parent : JavaScript
  • computing/javascript

Losing This

JavaScript doesn’t have bound method and we can easily lose this when we store or pass a method as a variable. We can use bind to fix this:

const ron = new Person("Ron");
const greet = ron.greet;
greet(); // Oops, this is undefined
const greet2 = ron.greet.bind(ron);
greet2(); // ok

When writing our own classes, we can even add some boilerplate code to automate that.

See Also

  • this
  • call()
  • apply()

Graph View

  • Losing This
  • See Also

Backlinks

  • JavaScript
  • mimic bound method in JavaScript

Created with Quartz v4.5.2 © 2026. All notes are written in Obsidian.

  • GitHub Repository
  • Mastodon