While Vadim and Corbin provided accurate explanations, allow me to elaborate further with some intricate details (I like using fancy words). The bind_1 function returns a new function that will always execute the specified function (f) with the defined arguments (o) as the context - essentially setting the context enables the usage of the this keyword within the function to refer to the designated context object. On the other hand, bind_2 can either return the function (f) with the provided context (o), or yield the result from executing the function (f) with the set context (o).
Another use for Function.prototype.bind is partial function application. For example, if you have a function where the context is irrelevant, you can predefine certain arguments using bind, making future calls more streamlined:
// defining a function with three parameters
function doSomething(person, action, message) {
return person + " " + action + " " + message + ".";
}
var shortcut = doSomething.bind(null, "Joshua", "says");
// the bind call here modifies doSomething by:
// 1. setting the context (_this_) to null
// 2. predefining arguments (person and action) as ("Joshua" and "says") respectively
// now we can simply use shortcut("Hello")
// instead of having to write doSomething("Joshua", "says", "Hello")
shortcut("Hello"); // outputs "Joshua says Hello."
The initial argument supplied to .apply(), .call(), or .bind() determines the context in which the function/method operates. This context corresponds to the value of the this keyword; therefore, inside the function, this refers to whatever is passed as the first argument. In this scenario, I used null since the function does not necessitate a specific context value; furthermore, null is shorter than undefined, and more preferable than utilizing random values like empty string ("") or empty objects ({}). Consequently, the remaining arguments are assigned as the initial set of function parameters.
function exampleContext() {
return this; // context during function execution
}
exampleContext(); // yields the global scope "window" (in a browser environment)
var exampleBind = exampleContext.bind(null);
exampleBind(); // results in "null"
var exampleBind = exampleContext.bind("Joshua");
exampleBind(); // produces "Joshua"
var exampleBind = exampleContext.bind({});
exampleBind(); // generates "Object {}"