incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nitin Borwankar <ni...@borwankar.com>
Subject Doug Crockford's typeOf vs usual js typeof
Date Wed, 26 Aug 2009 05:44:01 GMT
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 http://javascript.crockford.com/remedial.html

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.

Nitin




37% of all statistics are made up on the spot
-------------------------------------------------------------------------------------
Nitin Borwankar
nborwankar@gmail.com

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