Consider the following:
var o1 = {}
var O = function () {
return this
}
var o2 = new O()
var o3 = function() {}
var o4 = [o1, o1]
var output = [
[_.isObject(o1), _.isObject(o2), _.isObject(o3), _.isObject(o4)],
[_.isPlainObject(o1), _.isPlainObject(o2), _.isPlainObject(o3), _.isPlainObject(o4)],
[typeof o1 === 'object', typeof o2 === 'object', typeof o3 === 'object', typeof o4 === 'object'],
[o1 instanceof Array, o2 instanceof Array, o3 instanceof Array, o4 instanceof Array]
]
/* outputs:
[
[true,true,true,true],
[true,false,false,false],
[true,true,false,true],
[false,false,false,true]
]
*/
Clearly we can see that there is a disconnect between .isObject()
:
.isPlainObject()
:
And our good ol' trusty friend typeof x === 'object'
.
I have three questions:
- Was there a conscious design decision to make
.isObject
and.isPlainObject
behave differently than the native .js type checking? - If my first question is true, what was the design decision and what are the benefits of doing it this way?
- Is there any native lodash (or underscore.js)
is*
function that behaves exactly the same astypeof x === 'object'
?
Obviously I can just continue to use typeof
, but syntactically it's a bit weird to use one or the other in some places, for example the usage of .isObject
will return false positives when checking for typeof x === 'object' && typeof x !== 'function'
. I don't really see any benefit of .isObject
returning true for functions when .isFunction
already exists.