Skip to content

Instantly share code, notes, and snippets.

@aharris
Created January 21, 2015 16:13
Show Gist options
  • Save aharris/9a9b9fe9a18bfcdcd2ff to your computer and use it in GitHub Desktop.
Save aharris/9a9b9fe9a18bfcdcd2ff to your computer and use it in GitHub Desktop.
Example usage of bind, call, and apply
var fx = function (arg1, arg2) {
console.log("___________________________________________");
console.log(this.use);
console.log(arg1, arg2);
console.log("___________________________________________");
};
var callObj = {
use: "Using Call"
};
// Arguments passed by parameters
fx.call(callObj, "bind arg 1", "bind arg 2");
// Arguments Passed by array
fx.apply({use: "Using Apply"}, ["apply arg 1", "apply arg 2"]);
//Creates an exact copy of the function with this specified correctly
var fxbound = fx.bind({use: "Using Bind"}, "bind first param", "bind second param");
fxbound();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment