{"record":{"id":"64565c97b38ccd66","repo":"jashkenas/underscore","slug":"bind-must-be-called-on-a-function","errorCode":null,"errorMessage":"Bind must be called on a function","messagePattern":"Bind must be called on a function","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"modules/bind.js","lineNumber":8,"sourceCode":"import restArguments from './restArguments.js';\nimport isFunction from './isFunction.js';\nimport executeBound from './_executeBound.js';\n\n// Create a function bound to a given object (assigning `this`, and arguments,\n// optionally).\nexport default restArguments(function(func, context, args) {\n  if (!isFunction(func)) throw new TypeError('Bind must be called on a function');\n  var bound = restArguments(function(callArgs) {\n    return executeBound(func, bound, context, this, args.concat(callArgs));\n  });\n  return bound;\n});\n","sourceCodeStart":1,"sourceCodeEnd":14,"githubUrl":"https://github.com/jashkenas/underscore/blob/e70d5bd070f1d883b40e786a955a61e4f4b3c2c6/modules/bind.js#L1-L14","documentation":"_.bind (and its OO counterpart) requires its first argument to be an actual function, since it creates a new function wrapping it. The library checks the argument with isFunction and throws a TypeError when it is not, because there is no meaningful way to bind a non-callable value.","triggerScenarios":"Calling _.bind with a non-function first argument, e.g. _.bind(undefined, obj), _.bind(obj.methodName, obj) when the method name is misspelled or the property is undefined/null, or passing a string/number/array instead of a function.","commonSituations":"Refactoring renamed or deleted an object method while call sites still reference it; a lookup like handlers[name] returns undefined because the registry key doesn't exist; destructured or optional callback arguments are undefined when the caller omits them; CommonJS/ESM import mistakes yield the wrong export object instead of a function.","solutions":["Log or console.log the value passed as the first argument to _.bind right before the call to see what it actually is (undefined, null, object, etc.).","Check that the method name/key you resolve to a function actually exists on the object (use obj.hasOwnProperty or a default).","Fix misspelled method names or restore the deleted/renamed function being bound.","Verify the import: ensure you imported the function itself, not a namespace/module object.","If the value may legitimately be absent, guard with typeof v === 'function' before binding."],"exampleFix":"// before\n_.bind(obj.onComplete, obj);\n// TypeError: Bind must be called on a function\n\n// after\nif (typeof obj.onComplete === 'function') {\n  _.bind(obj.onComplete, obj);\n}","handlingStrategy":"type-guard","validationCode":"function assertBindable(fn) {\n  if (typeof fn !== 'function') {\n    throw new TypeError('_.bind requires a function, got: ' + typeof fn);\n  }\n}\n// call before: assertBindable(obj.onComplete); _.bind(obj.onComplete, obj);","typeGuard":"const isBindable = (v) => typeof v === 'function';","tryCatchPattern":"try {\n  const bound = _.bind(maybeFn, ctx);\n  return bound;\n} catch (e) {\n  if (e instanceof TypeError && /Bind must be called on a function/.test(e.message)) {\n    return _.noop; // or log and fall back\n  }\n  throw e;\n}","preventionTips":["Always typeof-check the value before _.bind, especially for dynamically resolved method names.","Use optional chaining/default parameters for optional callbacks: typeof cb === 'function' ? _.bind(cb, ctx) : null.","Keep object method names in one constants module to avoid typo drift between definition and bindAll/bind call sites.","Verify your bundler/import setup so a module namespace object is not accidentally passed instead of the exported function.","Add a unit test for each dynamically-resolved handler to assert it is a function."],"tags":["typeerror","type-check","functions","argument-validation"],"backgroundTag":"not-a-function","analyzedSha":"e70d5bd070f1d883b40e786a955a61e4f4b3c2c6","analyzedAt":"2026-08-29T10:08:07.718Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}