Search code examples
javascriptnode.jsunderscore.jslodashmixins

Verify that a provided value is an instance of Lodash


Im working on a JS module that requires an instance of Lodash to be handed to it. I want to check that the variable handed to it is in fact an instance of lodash, but i'm having a hard time doing that....

Heres the console output of me trying various ways to get it:

_.all()
true
_.identity
Ut(n){return n}
_.identity.name
"Ut"
_.name
"J"
_.prototype.name
undefined
_.prototype.constructor.name
"J"
_.findIndex.prototype.name
undefined
_.constructor.isPrototypeOf(_)
false
_.isPrototypeOf(_)
false
_.isPrototypeOf(_.constructor)
false
_.isPrototypeOf(_.prototype)
false
_.constructor.prototype.name
""
_.prototype.name
undefined
_.isPrototypeOf( new _ )
false
_.__proto__.__proto__.constructor.name
"Object"
_.__proto__.constructor.name
"Function"
_.constructor.name
"Function"
_.prototype.constructor.name
"Ot"

Now if check if _ is an instance of _.constructor, like so:

_ instanceof _.constructor
true

That will obviously say yes. But thats like asking "Are you... you?.."

So is there a way to check if a specific variable is an instance of Lodash?

Thanks!


Solution

  • Maybe the best solution is to go for the version information, could save you some trouble with the differences between versions as well. Other than that, what I've seen is people looking for some specific methods they expect to find on the library.