Binding Arguments with Bind

By  on  

One of my favorite and most essential Function method is bind, a function we added to MooTools when it wasn't featured in the JavaScript language itself. We often think of using bind to simply bind a method's call to its host object, but did you know you can also bind arguments with the host object?

You've probably done something like this:

this._onTargetAvailable = this._onTargetAvailable.bind(this);

That pattern is frequently used, especially in classed-based code or when passing callback functions. What you may not often see is bound arguments:

this._onTargetAvailable = this._onTargetAvailable.bind(
    this,
    arg1,
    arg2,
    arg3
);

Binding arguments gives you more power in how your bound function is used! Whenever onTargetAvailable is called, the arguments you provide will be in that order, and any additional arguments will be added to the end of the argument list!

Recent Features

  • By
    CSS Filters

    CSS filter support recently landed within WebKit nightlies. CSS filters provide a method for modifying the rendering of a basic DOM element, image, or video. CSS filters allow for blurring, warping, and modifying the color intensity of elements. Let's have...

  • By
    Introducing MooTools Templated

    One major problem with creating UI components with the MooTools JavaScript framework is that there isn't a great way of allowing customization of template and ease of node creation. As of today, there are two ways of creating: new Element Madness The first way to create UI-driven...

Incredible Demos

  • By
    Creating the Treehouse Frog Animation

    Before we start, I want to say thank you to David for giving me this awesome opportunity to share this experience with you guys and say that I'm really flattered. I think that CSS animations are really great. When I first learned how CSS...

  • By
    iPad Detection Using JavaScript or PHP

    The hottest device out there right now seems to be the iPad. iPad this, iPad that, iPod your mom. I'm underwhelmed with the device but that doesn't mean I shouldn't try to account for such devices on the websites I create. In Apple's...

Discussion

    Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!