验证提供的值是否为Lodash的实例

时间:2016-09-26 20:19:22

标签: javascript node.js underscore.js lodash mixins

我正在研究一个需要Lodash实例的JS模块。我想检查传递给它的变量实际上是lodash的一个实例,但是我很难做到这一点....

下面是我尝试各种方法获得它的控制台输出:

_.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"

现在,如果检查__.constructor的实例,请执行以下操作:

_ instanceof _.constructor
true

显然会说是的。但那就像问“你......你?......”

那么有没有办法检查特定变量是否是Lodash的一个实例?

谢谢!

1 个答案:

答案 0 :(得分:1)

也许最好的解决方案是获取version信息,也可以为版本之间的差异省去一些麻烦。除此之外,我所看到的是人们正在寻找他们期望在图书馆找到的特定方法。

相关问题