CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-lodash-bind

The lodash method `_.bind` exported as a module.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

Lodash Bind

The lodash method _.bind exported as a standalone module. This function creates a function that invokes func with the this binding of thisArg and prepends any additional arguments to those provided to the bound function.

Package Information

  • Package Name: lodash.bind
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install lodash.bind

Core Imports

const bind = require('lodash.bind');

For ES modules:

import bind from 'lodash.bind';

Basic Usage

const bind = require('lodash.bind');

function greet(greeting, punctuation) {
  return greeting + ' ' + this.user + punctuation;
}

const object = { user: 'fred' };

// Simple binding
const bound = bind(greet, object, 'hi');
bound('!');
// => 'hi fred!'

// With placeholder arguments
const bound2 = bind(greet, object, bind.placeholder, '!');
bound2('hello');
// => 'hello fred!'

Capabilities

Function Binding

Creates a function that invokes the original function with a specific this context and optionally pre-applied arguments.

/**
 * Creates a function that invokes `func` with the `this` binding of `thisArg`
 * and prepends any additional `bind` arguments to those provided to the
 * bound function.
 *
 * @param {Function} func The function to bind.
 * @param {*} thisArg The `this` binding of `func`.
 * @param {...*} [partials] The arguments to be partially applied.
 * @returns {Function} Returns the new bound function.
 */
function bind(func, thisArg, ...partials);

Usage Examples:

const bind = require('lodash.bind');

// Basic binding
function sayHello() {
  return 'Hello, ' + this.name;
}

const person = { name: 'Alice' };
const boundSayHello = bind(sayHello, person);
boundSayHello(); // => 'Hello, Alice'

// Partial application
function add(a, b, c) {
  return a + b + c;
}

const addTwo = bind(add, null, 2);
addTwo(3, 4); // => 9 (2 + 3 + 4)

// Placeholder usage
const addToFive = bind(add, null, bind.placeholder, 5);
addToFive(3, 2); // => 10 (3 + 5 + 2)

Placeholder Support

The bind function supports placeholder arguments using bind.placeholder.

/**
 * The placeholder value used for partially applied arguments.
 * Defaults to the bind function itself.
 */
bind.placeholder;

Usage Examples:

const bind = require('lodash.bind');

function multiply(a, b, c) {
  return a * b * c;
}

// Skip the first argument using placeholder
const multiplyBy2And3 = bind(multiply, null, bind.placeholder, 2, 3);
multiplyBy2And3(4); // => 24 (4 * 2 * 3)

// Multiple placeholders
const customMultiply = bind(multiply, null, bind.placeholder, bind.placeholder, 5);
customMultiply(2, 3); // => 30 (2 * 3 * 5)

Key Features

  • Enhanced Function Binding: More powerful than native Function.prototype.bind
  • Partial Application: Pre-apply arguments to create specialized functions
  • Placeholder Support: Skip arguments using placeholders for flexible argument patterns
  • Consistent Behavior: Reliable cross-browser compatibility
  • No Length Property: Unlike native bind, doesn't set the "length" property of bound functions

Error Handling

The bind function will throw a TypeError if the first argument is not a function:

const bind = require('lodash.bind');

try {
  bind('not a function', {});
} catch (error) {
  console.log(error.message); // => "Expected a function"
}

Differences from Native bind

  • Placeholder Support: Native bind doesn't support placeholders
  • Length Property: Native bind sets the length property, lodash bind doesn't
  • Performance: Lodash bind may have different performance characteristics
  • Cross-browser Consistency: Lodash bind provides consistent behavior across environments

Types

/**
 * The bind function type definition
 */
interface BindFunction {
  <T extends Function>(func: T, thisArg: any, ...partials: any[]): T;
  placeholder: any;
}
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/lodash.bind@4.2.x
Publish Source
CLI
Badge
tessl/npm-lodash-bind badge