JS Flashcards
1
Q
bind
A
when referencing a function that uses this.
, set this
to the first arg, then the remaining params to the param array
function.bind(thisArg[, arg1[, arg2[, …]]])
Returns a new function that, when called, will have this equal to thisArg, the first parameter equal to param1, the second parameter equal to param2, etc.
var Button = function(content) { this.content = content; }; Button.prototype.click = function() { console.log(this.content + ' clicked'); }
var myButton = new Button('OK'); myButton.click();
var looseClick = myButton.click; looseClick();
var boundClick = myButton.click.bind(myButton); boundClick();
// Example showing binding some parameters var sum = function(a, b) { return a + b; };