What is an Object Type(O)?

Juriy Zaytsev kangax at gmail.com
Fri Mar 6 21:00:47 PST 2009


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);
}

and then simply checked for `isPrimitive` being false before  
proceeding (otherwise throwing a TypeError). It just seemed like the  
most straight forward way of doing it.

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;
}

The latter example assumed meaning of "Object" literally and would  
skip, say, Function objects, or MSHTML's host objects returning  
"unknown" as their `typeof`.

I started looking into specs more thoroughly to find out what Type  
really is:

5.2 says: 'Type(x) is used as shorthand for "the type of x"'

What's type of x, then?

Type (4.3.1) says:
"A type is a set of data values as defined in section 8 of this  
specification."

Section 8, in its turn, lists - The Undefined Type, The Null  
Type, ... , and finally The Object Type (8.6)

8.6 says:
"An Object is a collection of properties. Each property is either a  
named data property, a named accessor property, or an internal  
property."

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? Does the former test - `!isPrimitive(o)` - look like a  
reasonable simulation of what ES3.1 says?


Thank you.

-- 
Juriy Zaytsev
Prototype.js core developer
kangax at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20090307/1cbc0e94/attachment.html>


More information about the Es-discuss mailing list