How Lodash isObjectLike Handles Functions
The Lodash library provides several utility functions to inspect data
types, among which _.isObjectLike is specifically designed
to identify object-like values while excluding primitives,
null, and executable functions. This article examines the
explicit exclusion rules _.isObjectLike applies to function
references, details the underlying JavaScript type checks responsible
for this behavior, and contrasts the method with
_.isObject.
The Core
Exclusion Rule: The typeof Evaluation
At its core, _.isObjectLike checks whether a value is
not null and has a typeof evaluation strictly
equal to "object". The exact internal implementation in
Lodash is:
function isObjectLike(value) {
return typeof value === 'object' && value !== null;
}Because of this strict definition, any value that yields a
typeof result other than "object" is
immediately rejected. In JavaScript, all executable function references
return "function" when evaluated by the typeof
operator. Consequently, functions fail the
typeof value === 'object' condition and are excluded by
default.
Excluded Function Types
The typeof check applies universally across the
JavaScript runtime to all variations of executable callables. As a
result, _.isObjectLike returns false for all
of the following:
- Standard Functions: Traditional function
declarations and function expressions (
function() {}). - Arrow Functions: ES6 arrow syntax
(
() => {}). - Async Functions: Functions declared with the
asynckeyword (async function() {}). - Generator Functions: Generator declarations
(
function*() {}). - ES6 Classes: Classes defined with the
classsyntax, which are evaluated internally as constructor functions (class MyClass {}). - Built-in Constructors: Native constructors such as
Object,Array,Function, andDate.
Even though functions in JavaScript are first-class objects capable
of holding properties, prototype chains, and custom methods,
_.isObjectLike deliberately ignores these object
characteristics in favor of the language's native
"function" type tag.
Comparison:
_.isObjectLike vs. _.isObject
The exclusion of function references is the defining difference
between _.isObjectLike and Lodash’s standard
_.isObject utility.
_.isObject(value): Follows the ECMAScript language specification for what qualifies as an object. It checksvalue != null && (typeof value === 'object' || typeof value === 'function'). Under this rule, executable functions returntrue._.isObjectLike(value): Targets values that behave structurally like objects (such as plain objects, arrays, and regex instances) without being callable. Because functions are callable invocations rather than structural data containers, they are filtered out.
If an application requires checking for structural records or
dictionaries while ensuring the target cannot be invoked as an
executable routine, _.isObjectLike acts as a guard against
passing function references.