Back to Functions

bind

function

Creates a function that invokes func with the this binding of thisArg and partials prepended to the arguments it receives.

Installation

Import
import { bind } from '@tulx/utils';

Source Code

Implementation
/**
 * Creates a function that invokes func with the this binding of thisArg and partials prepended to the arguments it receives.
 *
 * @param func - The function to bind.
 * @param thisArg - The this binding of func.
 * @param partials - The arguments to be partially applied.
 * @returns Returns the new bound function.
 *
 * @example
 * ```ts
 * const greet = function(greeting: string, punctuation: string) {
 *   return greeting + ' ' + this.user + punctuation;
 * };
 * const object = { 'user': 'fred' };
 * const bound = bind(greet, object, 'hi');
 * bound('!'); // 'hi fred!'
 * ```
 */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function bind<T extends (...args: any[]) => any>(
  func: T,
  thisArg: unknown,
  ...partials: readonly unknown[]
): (...args: unknown[]) => ReturnType<T> {
  return function (this: unknown, ...args: unknown[]): ReturnType<T> {
    return func.apply(thisArg, [...partials, ...args]) as ReturnType<T>;
  };
}

Example

import { bind } from '@tulx/utils';

const greet = function(greeting: string, punctuation: string) {
return greeting + ' ' + this.user + punctuation;
};
const object = { 'user': 'fred' };
const bound = bind(greet, object, 'hi');
bound('!'); // 'hi fred!'

Related Functions