You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Socket
Socket
Sign inDemoInstall

call-bind

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

call-bind

Robustly `.call.bind()` a function


Version published
Weekly downloads
40M
decreased by-18.31%
Maintainers
1
Install size
226 kB
Created
Weekly downloads
 

Package description

What is call-bind?

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.

What are call-bind's main functionalities?

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

Other packages similar to call-bind

Changelog

Source

v1.0.7 - 2024-02-12

Commits

  • [Refactor] use es-define-property 09b76a0
  • [Deps] update get-intrinsic, set-function-length ad5136d

Readme

Source

call-bind Version Badge

github actions coverage dependency status dev dependency status License Downloads

npm badge

Robustly .call.bind() a function.

Getting started

npm install --save call-bind

Usage/Examples

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]);

Tests

Clone the repo, npm install, and run npm test

Keywords

FAQs

Package last updated on 13 Feb 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc