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.