incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jesse Hallett <>
Subject Re: Doug Crockford's typeOf vs usual js typeof
Date Wed, 26 Aug 2009 15:31:53 GMT
A simpler test is:

    [1,2,3] instanceof Array

Though as Crockford points out that test may not be as reliable as `typeOf`
in some cases.  The scenario in which Crockford says `typeOf` will work
better is when to object being tested was created in a different window or
frame.  That case may not apply to CouchDB views.

On Aug 25, 2009 10:44 PM, "Nitin Borwankar" <> wrote:

Hi Damien,JChris,JanL et al,

As a js newbie I recently hit the WTF? wall when I discovered
typeof([1,2,3]) = object rather than the expected array.

Doug Crockford suggests a custom typeOf function (note the Cap O) in his
remedial javascript

function typeOf(value) {
   var s = typeof value;
   if (s === 'object') {
       if (value) {
           if (typeof value.length === 'number' &&
                   !(value.propertyIsEnumerable('length')) &&
                   typeof value.splice === 'function') {
               s = 'array';
       } else {
           s = 'null';
   return s;

Given that we may want to deal with arrays and hashes differently in map
fucntions, is it possible to add this typeOf fucntion to the standard couch
js libs?
This would allow this very useful functionality to be accessed in map
functions and lucene index functions without having to add these inline or
via couchapp.


37% of all statistics are made up on the spot
Nitin Borwankar

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message