Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Passing a function with arguments as an argument?

Is it possible to pass a javascript function with arguments as an argument?

Example:

$(edit_link).click( changeViewMode( myvar ) );

Answer*

Draft saved
Draft discarded
Cancel
4
  • Thanks, this helped me. Used it like this: $.ajax(url).done(handler.bind(this, var1, var2)); Commented Jun 16, 2016 at 10:13
  • 2
    Which option is best ? Using closure or using the bind function ? Please let me know if there is any performance impact Commented Sep 7, 2016 at 8:50
  • 5
    @Varun See Why is bind slower than a closure? Commented Sep 7, 2016 at 13:48
  • I've been using fn.bind(this) to resolve the dreaded this confusion in callback functions without understanding the broader purpose of bind – but now I get it! Thanks 🙏 Commented Jun 7, 2022 at 5:58

lang-js

AltStyle によって変換されたページ (->オリジナル) /