What is an Object Type(O)?

David-Sarah Hopwood david.hopwood at industrial-designers.co.uk
Sat Mar 7 07:42:39 PST 2009


Juriy Zaytsev wrote:
> When implementing ES3.1 `Object.keys` in ES3, one of the steps in
> algorithm of `Object.keys` seemed a bit confusing:
> 
> ...
> 1. If the Type(O) is not Object, throw a TypeError exception.
> ...
> 
> Based on my understanding, I implemented this check as:
> 
> function isPrimitive(o) {
>   return o == null || /^(boolean|number|string)$/.test(typeof o);
> }

Yes, that's correct.

('o == null' is non-obviously equivalent to
'o === null || o === undefined'.)

> I then received a response from one of our developers that `isObject`
> could probably be implemented as:
> 
> function isObject(o) {
>   return typeof o === 'object' && o !== null;
> }

That's wrong; host objects and functions have Type Object.

> Am I correct in my understanding that when specs say Type(O) should be
> an Object, this implies that a value is not an instance of `Object` but
> an object in a sense of a collection of properties, and that, for
> example, a Function object should be considered an Object type as well?

Yes.

-- 
David-Sarah Hopwood ⚥



More information about the Es-discuss mailing list