This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

Understanding function bind() in JavaScript

3:03 JavaScript lesson by

Changing context in JavaScript is only possible using call() and apply(). The bind() method on the function prototype use those methods to allow us to quickly change the context in which a function is executed in. Learn how bind works by recreating it.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

Changing context in JavaScript is only possible using call() and apply(). The bind() method on the function prototype use those methods to allow us to quickly change the context in which a function is executed in. Learn how bind works by recreating it.

Avatar
Sumit

Hi,

In the code snippet shown in the video you have divided arguments into two arrays, innerArgs and outerArgs can you please explain it a bit further

In this lesson, we're going to talk about function bind and how it works. We'll also recreate it. Right here, I have this function bark, which we'll count as [indecipherable 00:09] dog bark noise. Bark looks like a free flowing function, but there are actually no free flowing functions in JavaScript. They're all attached to objects.

In this case, because there's nothing rising in .notations [indecipherable 00:17] to the left, the bark function's actually attached to some global objects and JSPIN has created that for us. Unless that global object has a bark noise property, that's going to be undefined. What we want to do is be able the call the bark function in the context of dog so it actually does say the bark noise, which is this string here.

If I run this without changing the context, you'll see we get undefined. But we actually have this .bind method on the functions that it can actually pass in a different context. That's going to return a function that we can invoke. Now, if I run it, we get the bark, bark, bark. We're going to recreate that to see how it works.

What we'll do is we'll make a new method on the function prototype here, and let's call it glue, so we can see how this works. The first thing we want to do is we know this is going to take in a context, because that's the thing that we want to do actually. We have that, and then we actually want to go ahead and save a reference to the actual function that we're trying to call.

We'll say that. That's actually going to be this. Then for any possible arguments that may or may not have been passed into this call to glue, we want to reference those.

We'll call this outer args. The reason I'm naming this outer args is because the glue or the bind method actually returns a function, which may have args, too. We need a way to differentiate between them, too.

I'll do that .slice.call on the arguments array and everything after the context, if anything. Then now, I can go ahead and return that function that we're going to call. This function's going to be given a new context here.

I want to grab any possible arguments that were passed in here. We can say inner args, and it's going to be the same thing, .slice.call arguments. You can pretty much make a copy of that.

Then what I can do now is I can say fn, which is our function, and, in this case, I'm going to use apply because we already have the array of arguments and apply takes an array. The context that I want to pass in was the one that was passed in to the original method, and then pass in the composed argument. I can say .concat.

The first thing I want to pass in is the outer args because those, if they were given, they were given first, and then the inner args. Now, if we come down here, if we say bark.glue, pass in dog, we immediately invoke just like bind, we run this, and we get bark, bark, bark. That's how a function bind works.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?