Created
January 21, 2015 16:13
-
-
Save aharris/9a9b9fe9a18bfcdcd2ff to your computer and use it in GitHub Desktop.
Example usage of bind, call, and apply
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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