jasnell / proposal-istypes

TC-39 Proposal for additional is{Type} APIs
201 stars 7 forks source link

What's the meaning of Object.isObject? #16

Closed lygstate closed 7 years ago

lygstate commented 7 years ago

Does the following pollyflil works?

// http://stackoverflow.com/questions/8511281/check-if-a-value-is-an-object-in-javascript
module.exports = (item) => {
  return (typeof item === 'object' && !Array.isArray(item) && item !== null)
}
ljharb commented 7 years ago

There's really only two possible meanings imo (based on common conventions) - item && (typeof item === 'object' || typeof item === 'function') (in other words, what the spec considers a Type "Object", aka a non-primitive), or item && typeof item === 'object' && !Array.isArray(item) (what convention calls a "plain object").

I feel like the former would be a better choice, since it would precisely match all the places in the spec that check if "Type(x) is Object".

lygstate commented 7 years ago

Yeap, I prefer the former version.

jasnell commented 7 years ago

I believe the current proposal handles this appropriately