bind()
Function
instance method
JS Home <<
JS Reference <<
Function <<
bind()
Description
Creates a new function that when called sets its this
keyword to the provided value, with a given sequence of arguments that precede any provided on function call.
Syntax
Signature | Description |
---|---|
aFunction.bind(thisArg[, arg1[, arg2[, ...argN]]]) | Creates a new function with a given this value and arguments provided individually. |
Parameters
Parameter | Description |
---|---|
thisArg | The value of this provided for the call to the function. |
arg1,arg2,...argN | The arguments with which the function should be called, null or undefined if no arguments required. |
Examples
Below is an example of using the bind()
method.
const module = {
str: ": A String was returned",
getString: function() {
return this.str;
}
};
// The function gets invoked at the global scope
const unboundGetString = module.getString;
alert(unboundGetString() + ": expected output: undefined");
const boundGetString = unboundGetString.bind(module);
alert(boundGetString() + ": expected output: A String was returned");
Related Tutorials
JavaScript Intermediate Tutorials - Lesson 8 - Functions