https://github.com/xotic750/bind-x
Creates a new function with a bound sequence of arguments.
https://github.com/xotic750/bind-x
bind browser function javascript nodejs
Last synced: about 2 months ago
JSON representation
Creates a new function with a bound sequence of arguments.
- Host: GitHub
- URL: https://github.com/xotic750/bind-x
- Owner: Xotic750
- License: mit
- Created: 2017-06-09T21:29:35.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2023-01-04T21:54:50.000Z (over 3 years ago)
- Last Synced: 2025-02-18T09:38:49.376Z (over 1 year ago)
- Topics: bind, browser, function, javascript, nodejs
- Language: JavaScript
- Homepage:
- Size: 2.55 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 21
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## bind-x
Creates a new function with a bound sequence of arguments.
### `module.exports` ⇒ function ⏏
The bind() method creates a new function that, when called, has its this
keyword set to the provided value, with a given sequence of arguments
preceding any provided when the new function is called.
**Kind**: Exported member
**Returns**: function - The bound function.
**Throws**:
- TypeError If target is not a function.
| Param | Type | Description |
| ------- | --------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| target | function | The target function. |
| thisArg | \* | The value to be passed as the this parameter to the target function when the bound function is called. The value is ignored if the bound function is constructed using the new operator. |
| [args] | ...\* | Arguments to prepend to arguments provided to the bound function when invoking the target function. |
**Example**
```js
import bind from 'bind-x';
this.x = 9; // this refers to global "window" object here in the browser
const module = {
x: 81,
getX: function() {
return this.x;
},
};
console.log(module.getX()); // 81
const retrieveX = module.getX;
console.log(retrieveX());
// returns 9 - The function gets invoked at the global scope
// Create a new function with 'this' bound to module
// New programmers might confuse the
// global var x with module's property x
const boundGetX = bind(retrieveX, module);
console.log(boundGetX()); // 81
```