You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Confusing documentation or bad implementation for util.isObject #743
Either the docs should be updated to clarify the behavior, or the implementation should be fixed. I would vote for the latter, otherwise the function name is confusing.
Kinda, but it's generally a bad sign when the documentation basically rephrases the source code. It would be better to change the implementation to check whether Type(arg) is Object, to be consistent with the other util.is* functions.
The implementation does check whether typeof arg is object, and in the case of a function, it isn't. Both the underscore and lodash implementations of isObject() have additional checks to return true when the argument is a function, since that's the expected behavior. So I also think changing the implementation is more useful than changing the docs here.
The documentation for util.isObject doesn't state that it returns false for functions, so I would consider it a bug fix. Besides, util.isObject didn't even officially exist before io.js v1.0.0, so it's unlikely a lot of people are relying on this broken behavior.
I do not think that the expected behavior for everyone is to return true for functions. There is util.isFunction for that. util.isObject can be used to check function arguments and the common case is to differentiate functions from plain objects.
IMO if there is a "bug" to fix, it is in the doc.
Activity
cjihrig commentedon Feb 6, 2015
It checks
arg !== null && typeof arg === 'object'to be exact.EDIT: I think "strictly" is thrown in there because
typeofreturns'object'fornull.vkurchatkin commentedon Feb 6, 2015
yeah, the wording is really confusing
cjihrig commentedon Feb 6, 2015
Would this work for you:
seishun commentedon Feb 6, 2015
Kinda, but it's generally a bad sign when the documentation basically rephrases the source code. It would be better to change the implementation to check whether Type(arg) is Object, to be consistent with the other
util.is*functions.ceejbot commentedon Feb 12, 2015
The implementation does check whether
typeof argis object, and in the case of a function, it isn't. Both the underscore and lodash implementations ofisObject()have additional checks to return true when the argument is a function, since that's the expected behavior. So I also think changing the implementation is more useful than changing the docs here.ceejbot commentedon Feb 12, 2015
(Util is frozen, I note, so doc change it'll have to be.)
vkurchatkin commentedon Feb 12, 2015
@ceejbot it's not about util being frozen, but about semver. Bumping major version because of
isObjectone-liner is kind of funnyseishun commentedon Feb 12, 2015
The documentation for
util.isObjectdoesn't state that it returnsfalsefor functions, so I would consider it a bug fix. Besides,util.isObjectdidn't even officially exist before io.js v1.0.0, so it's unlikely a lot of people are relying on this broken behavior.targos commentedon Feb 12, 2015
I do not think that the expected behavior for everyone is to return true for functions. There is
util.isFunctionfor that.util.isObjectcan be used to check function arguments and the common case is to differentiate functions from plain objects.IMO if there is a "bug" to fix, it is in the doc.
seishun commentedon Feb 12, 2015
@targos
It's the expected behavior for anyone who knows what an object is.
util.isPrimitivereturnstruefor all primitives,util.isStringreturnstruejust for strings. It's the same thing with objects and functions.That's what
util.isFunctionis for.util: fixed behavior of isObject()
trevnorris commentedon Feb 12, 2015
IMO the docs should change to explain that an Object is anything that isn't a primitive. Though that probably doesn't clear up a lot.
doc: fix util.isObject documentation
Fishrock123 commentedon Mar 31, 2015
Fixed by a doc fix in 245ba1d
See later comments in #822 (comment) for doc vs functionality part.