A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://webplatform.github.io/docs/javascript/Function/bind below:

bind ยท WebPlatform Docs

bind Summary

For a given function, creates a bound function that has the same body as the original function. In the bound function, the this object resolves to the passed in object. The bound function has the specified initial parameters.

Syntax
function.bind( thisArg [, arg1 [, arg2 [, argN ]]])
function
Required. A function object.
thisArg
Required. An object to which the this keyword can refer inside the new function.
arg1 [, arg2 [, argN ]]]
Optional. A list of arguments to be passed to the new function.
Return Value

A new function that is the same as the function function, except for the thisArg object and the initial arguments.

Examples

The following code shows how to use the bind method.


 var checkNumericRange = function (value) {
     if (typeof value !== 'number')
         return false;
     else
         return value >= this.minimum && value <= this.maximum;
 }

 
 var range = { minimum: 10, maximum: 20 };

 
 var boundCheckNumericRange = checkNumericRange.bind(range);

 
 var result = boundCheckNumericRange (12);
 document.write(result);

 

In the following example, the thisArg object is different from the object that contains the original method.


 var originalObject = {
     minimum: 50,
     maximum: 100,
     checkNumericRange: function (value) {
         if (typeof value !== 'number')
             return false;
         else
             return value >= this.minimum && value <= this.maximum;
     }
 }

 
 var result = originalObject.checkNumericRange(10);
 document.write(result + " ");
 

 
 var range = { minimum: 10, maximum: 20 };

 
 var boundObjectWithRange = originalObject.checkNumericRange.bind(range);

 
 var result = boundObjectWithRange(10);
 document.write(result);
 

The following code shows how to use the arg1[,arg2[,argN]]] arguments. The bound function uses the parameters specified in the bind method as the first and second parameters. Any parameters specified when the bound function is called are used as the third, fourth (and so on) parameters.


 var displayArgs = function (val1, val2, val3, val4) {
     document.write(val1 + " " + val2 + " " + val3 + " " + val4);
 }

 var emptyObject = {};

 
 
 var displayArgs2 = displayArgs.bind(emptyObject, 12, "a");

 
 
 displayArgs2("b", "c");
 
Exceptions

If the specified function is not a function, a TypeError exception is thrown.

See also Other articles Attributions

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4