{"record":{"id":"e4cfad7d6914d86c","repo":"jashkenas/underscore","slug":"bindall-must-be-passed-function-names","errorCode":null,"errorMessage":"bindAll must be passed function names","messagePattern":"bindAll must be passed function names","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"modules/bindAll.js","lineNumber":11,"sourceCode":"import restArguments from './restArguments.js';\nimport flatten from './_flatten.js';\nimport bind from './bind.js';\n\n// Bind a number of an object's methods to that object. Remaining arguments\n// are the method names to be bound. Useful for ensuring that all callbacks\n// defined on an object belong to it.\nexport default restArguments(function(obj, keys) {\n  keys = flatten(keys, false, false);\n  var index = keys.length;\n  if (index < 1) throw new Error('bindAll must be passed function names');\n  while (index--) {\n    var key = keys[index];\n    obj[key] = bind(obj[key], obj);\n  }\n  return obj;\n});\n","sourceCodeStart":1,"sourceCodeEnd":18,"githubUrl":"https://github.com/jashkenas/underscore/blob/e70d5bd070f1d883b40e786a955a61e4f4b3c2c6/modules/bindAll.js#L1-L18","documentation":"_.bindAll binds each named method of an object to that object; the method names come from the remaining rest arguments. If, after flattening, no keys were supplied at all, there is nothing to bind, so the library throws. This is almost always a call-site mistake rather than a data problem.","triggerScenarios":"Calling _.bindAll(obj) with no method-name arguments, or _.bindAll(obj, []) or _.bindAll(obj, [], []) — any call whose flattened rest-argument list has length 0.","commonSituations":"Spreading a possibly-empty array of names: _.bindAll(obj, ...names); building the method-name list dynamically from a filter that matched nothing; migrating from code where defaults filled in the names and the defaults were removed; copying an example and forgetting to replace placeholder method names.","solutions":["Pass at least one method name: _.bindAll(obj, 'method1', 'method2') or an array _.bindAll(obj, names).","Verify the variable holding the names array is non-empty at the call site (console.log(names) before the call).","Fix the upstream filter/derivation that produced an empty list of names.","If names can legitimately be empty, skip the call when names.length === 0 instead of invoking bindAll.","Consider whether you intended to spread the array: _.bindAll(obj, ...names) instead of _.bindAll(obj, names) (both work here due to internal flatten, but an empty array in either form still throws)."],"exampleFix":"// before\n_.bindAll(view, view.eventNames); // eventNames is []\n// Error: bindAll must be passed function names\n\n// after\nif (view.eventNames.length > 0) {\n  _.bindAll(view, view.eventNames);\n}","handlingStrategy":"validation","validationCode":"function safeBindAll(obj, names) {\n  const flat = names.flat(Infinity);\n  if (!Array.isArray(names) || flat.length === 0) return obj; // nothing to bind\n  return _.bindAll(obj, ...flat);\n}","typeGuard":"const hasNamesToBind = (names) =>\n  Array.isArray(names) && names.flat(Infinity).length > 0;","tryCatchPattern":"try {\n  return _.bindAll(obj, ...names);\n} catch (e) {\n  if (e instanceof Error && /bindAll must be passed function names/.test(e.message)) {\n    return obj; // binding zero methods is a no-op\n  }\n  throw e;\n}","preventionTips":["Guard the names list length before calling bindAll: if (names.length) _.bindAll(obj, ...names).","Avoid building the method-name list with filters that can silently return [].","Default to an explicit set of method names in source rather than deriving them at runtime.","Remember bindAll throws on an empty list — treat it as a required argument.","Write a test that calls bindAll with the real name list to catch regressions where it becomes empty."],"tags":["argument-validation","empty-arguments","functions","this-binding"],"backgroundTag":"missing-required-argument","analyzedSha":"e70d5bd070f1d883b40e786a955a61e4f4b3c2c6","analyzedAt":"2026-08-29T10:08:07.718Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}