how to use the bindkey function from lodash in javascript

The bindKey function from lodash creates a function that invokes the method at a specified key path of an object, optionally currying any additional arguments provided to the bound function.

Here's an example usage:

index.tsx
const lodash = require('lodash');

const obj = {
  name: 'John',
  greet: function(greeting) {
    console.log(`${greeting}, I'm ${this.name}`);
  }
};

const boundFunc = lodash.bindKey(obj, 'greet', 'Hello');
boundFunc(); // output: 'Hello, I'm John'
252 chars
12 lines

In the example above, bindKey creates a new function boundFunc that invokes the greet method of obj with a first argument of 'Hello'. The bindKey function binds obj as the this value of greet so that the this.name property resolves to John.

Note that bindKey only works with functions that can be invoked via a this value. If the provided key path does not point to a function, bindKey will throw an error.

gistlibby LogSnag