Home

function: bindKey


bindKey(object: Object, key: string, partials: *): function

Creates a function that invokes the method at `object[key]` with `partials` prepended to the arguments it receives. This method differs from `bind` by allowing bound functions to reference methods that may be redefined or don't yet exist. See [Peter Michaux's article](http://peter.michaux.ca/articles/lazy-function-definition-pattern) for more details. The `bindKey.placeholder` value, which defaults to `_` in monolithic builds, may be used as a placeholder for partially applied arguments.

Example(s):

const object = {
  'user': 'fred',
  'greet': function(greeting, punctuation) {
    return greeting + ' ' + this.user + punctuation
  }
}

const bound = bindKey(object, 'greet', 'hi')
bound('!')
// => 'hi fred!'

object.greet = function(greeting, punctuation) {
  return greeting + 'ya ' + this.user + punctuation
}

bound('!')
// => 'hiya fred!'

// Bound with placeholders.
const bound = bindKey(object, 'greet', _, '!')
bound('hi')
// => 'hiya fred!'

Params:

Name Type Attribute Description
object The object to invoke the method on.
key The key of the method.
partials The arguments to be partially applied.

Returns:

function

  {
    "comment": "/**\r\n * Creates a function that invokes the method at `object[key]` with `partials`\r\n * prepended to the arguments it receives.\r\n *\r\n * This method differs from `bind` by allowing bound functions to reference\r\n * methods that may be redefined or don't yet exist. See\r\n * [Peter Michaux's article](http://peter.michaux.ca/articles/lazy-function-definition-pattern)\r\n * for more details.\r\n *\r\n * The `bindKey.placeholder` value, which defaults to `_` in monolithic\r\n * builds, may be used as a placeholder for partially applied arguments.\r\n *\r\n * @since 0.10.0\r\n * @category Function\r\n * @param {Object} object The object to invoke the method on.\r\n * @param {string} key The key of the method.\r\n * @param {...*} [partials] The arguments to be partially applied.\r\n * @returns {Function} Returns the new bound function.\r\n * @example\r\n *\r\n * const object = {\r\n *   'user': 'fred',\r\n *   'greet': function(greeting, punctuation) {\r\n *     return greeting + ' ' + this.user + punctuation\r\n *   }\r\n * }\r\n *\r\n * const bound = bindKey(object, 'greet', 'hi')\r\n * bound('!')\r\n * // => 'hi fred!'\r\n *\r\n * object.greet = function(greeting, punctuation) {\r\n *   return greeting + 'ya ' + this.user + punctuation\r\n * }\r\n *\r\n * bound('!')\r\n * // => 'hiya fred!'\r\n *\r\n * // Bound with placeholders.\r\n * const bound = bindKey(object, 'greet', _, '!')\r\n * bound('hi')\r\n * // => 'hiya fred!'\r\n */",
    "meta": {
        "range": [
            1685,
            1987
        ],
        "filename": "bindKey.js",
        "lineno": 53,
        "path": "C:\\Users\\beaujeup\\projects\\jsdoc-template\\examples\\lodash\\lodash-repo",
        "code": {
            "id": "astnode100000254",
            "name": "bindKey",
            "type": "FunctionDeclaration",
            "paramnames": [
                "object",
                "key",
                "partials"
            ]
        },
        "vars": {
            "holders": "bindKey~holders",
            "bitmask": "bindKey~bitmask"
        }
    },
    "description": "Creates a function that invokes the method at `object[key]` with `partials`\rprepended to the arguments it receives.\r\rThis method differs from `bind` by allowing bound functions to reference\rmethods that may be redefined or don't yet exist. See\r[Peter Michaux's article](http://peter.michaux.ca/articles/lazy-function-definition-pattern)\rfor more details.\r\rThe `bindKey.placeholder` value, which defaults to `_` in monolithic\rbuilds, may be used as a placeholder for partially applied arguments.",
    "since": "0.10.0",
    "tags": [
        {
            "originalTitle": "category",
            "title": "category",
            "text": "Function",
            "value": "Function"
        }
    ],
    "params": [
        {
            "type": {
                "names": [
                    "Object"
                ]
            },
            "description": "The object to invoke the method on.",
            "name": "object"
        },
        {
            "type": {
                "names": [
                    "string"
                ]
            },
            "description": "The key of the method.",
            "name": "key"
        },
        {
            "type": {
                "names": [
                    "*"
                ]
            },
            "optional": true,
            "variable": true,
            "description": "The arguments to be partially applied.",
            "name": "partials"
        }
    ],
    "returns": [
        {
            "type": {
                "names": [
                    "function"
                ]
            },
            "description": "Returns the new bound function."
        }
    ],
    "examples": [
        "const object = {\r  'user': 'fred',\r  'greet': function(greeting, punctuation) {\r    return greeting + ' ' + this.user + punctuation\r  }\r}\r\rconst bound = bindKey(object, 'greet', 'hi')\rbound('!')\r// => 'hi fred!'\r\robject.greet = function(greeting, punctuation) {\r  return greeting + 'ya ' + this.user + punctuation\r}\r\rbound('!')\r// => 'hiya fred!'\r\r// Bound with placeholders.\rconst bound = bindKey(object, 'greet', _, '!')\rbound('hi')\r// => 'hiya fred!'"
    ],
    "name": "bindKey",
    "longname": "bindKey",
    "kind": "function",
    "scope": "global",
    "___id": "T000002R000024",
    "___s": true,
    "filepath": "bindKey.js"
}