0.1.1 • Published 3 years ago

bind-function v0.1.1

Weekly downloads
5
License
ISC
Repository
github
Last release
3 years ago

Bind function

Passed a function f and zero or more arguments a1...an returns a function that, when called, calls and returns f with a1...an passed as arguments. Alternative to Function.prototype.bind that does not accept a this value.

API

Syntax

bind(func, [arg1, ... argn]);

Parameters

func

Required. The function to be bound.

args

Optional. Arguments to be passed to the argument function when called.

Return

A new function. Bound variant of the argument function.

Example:

import bind from 'bind-function';

const sumNums = (a, b) => a + b;

const boundSumNums = bind(sumNums, 1, 2);

boundSumNums(); // returns 3

Purpose

JavaScript has a native solution for function binding, namely Function.prototype.bind. Not only is this method syntactically verbose, in that it requires prototype reference and must accept a this binding, it does not always work as expected, partcicularly when working with onClick events. Bind function offers an alternative for when you need to call a function with some specified arguments.

Test

Tested with AVA.

npm test

or

npx ava