Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
The call-bind npm package is a utility that allows you to use the ECMAScript 5 .call and .apply methods with a fixed this value. It is particularly useful when you want to ensure that the context (the this value) of a function call is bound to a specific object, regardless of how the function is invoked. This can be helpful in functional programming and when dealing with higher-order functions that might change the context of a function call.
callBind
Provides a way to use the native call method in a bound fashion, ensuring the this value is fixed when invoking a function.
const callBind = require('call-bind');
const call = callBind.call;
const unboundSlice = Array.prototype.slice;
const slice = call(unboundSlice);
function example() {
const args = slice(arguments);
console.log(args);
}
example(1, 2, 3); // Logs: [1, 2, 3]
applyBind
Allows you to use the native apply method in a bound manner, similar to callBind but for functions that expect an array of arguments.
const callBind = require('call-bind');
const apply = callBind.apply;
const unboundForEach = Array.prototype.forEach;
const forEach = apply(unboundForEach);
function example(array, callback) {
forEach(array, callback);
}
example([1, 2, 3], function(element) { console.log(element); }); // Logs: 1, 2, 3
Implements the Function.prototype.bind method in an ECMAScript 5-compliant way. It is similar to call-bind in that it allows you to fix the this value for function calls, but it does so by returning a new function with the this value bound, rather than using the call or apply methods directly.
Robustly .call.bind()
a function.
npm install --save call-bind
const assert = require('assert');
const callBind = require('call-bind');
const callBound = require('call-bind/callBound');
function f(a, b) {
assert.equal(this, 1);
assert.equal(a, 2);
assert.equal(b, 3);
assert.equal(arguments.length, 2);
}
const fBound = callBind(f);
const slice = callBound('Array.prototype.slice');
delete Function.prototype.call;
delete Function.prototype.bind;
fBound(1, 2, 3);
assert.deepEqual(slice([1, 2, 3, 4], 1, -1), [2, 3]);
Clone the repo, npm install
, and run npm test
v1.0.3 - 2023-10-19
a994df6
npmignore
to autogenerate an npmignore file eef3ef2
1845ccf
node/install
instead of node/run
; use codecov
action 5b47d53
set-function-length
a0e165c
@ljharb/eslint-config
, aud
, tape
9c50103
019c6d0
eslint
, @ljharb/eslint-config
, aud
, auto-changelog
, safe-publish-latest
, tape
23bd718
62552d7
eslint
, @ljharb/eslint-config
, aud
, auto-changelog
, tape
ec81665
eslint
, @ljharb/eslint-config
, safe-publish-latest
, tape
35d67fc
eslint
, @ljharb/eslint-config
, aud
, tape
0266d8d
@ljharb/eslint-config
, aud
, tape
43a5b28
define-data-property
, function-bind
, get-intrinsic
780eb36
aud
, tape
90d50ad
prepublishOnly
script for npm 7+ 44c5433
get-intrinsic
86bfbfc
get-intrinsic
5c53354
4c393a8
get-intrinsic
4e70bde
get-intrinsic
55ae803
FAQs
Robustly `.call.bind()` a function
The npm package call-bind receives a total of 40,198,729 weekly downloads. As such, call-bind popularity was classified as popular.
We found that call-bind demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.