var dbj = {} ;
 dbj.isWhat = function(o) { return Object.prototype.toString.call
(o).match(/\w+/g)[1]; }

// optimization
dbj.TYPENAMES = { 'obj' : dbj.isWhat({}), 'arr' : dbj.isWhat([]),
                 'fun' : dbj.isWhat(Function()), 'str' : dbj.isWhat
(""),
                 'num'   : dbj.isWhat(1) } ;

                 dbj.isObject = function(x) { return dbj.isWhat(x) ===
dbj.TYPENAMES.obj; }
                 dbj.isArray = function(x) { return dbj.isWhat(x) ===
dbj.TYPENAMES.arr; }
                 dbj.isFunction = function(x) { return dbj.isWhat(x)
=== dbj.TYPENAMES.fun; }
                 dbj.isString = function(x) { return dbj.isWhat(x) ===
dbj.TYPENAMES.str; }
                 dbj.isNumber = function(x) { return dbj.isWhat(x) ===
dbj.TYPENAMES.num; }

Above will work equaly well without regexp inside isWhat(), but , I
like to have a typename as 'Object' rather than '[object Object]'. You
never know which browser/OS combination will break the rules and
return some slightly different format from Object.prototype.toString
()...

All of this is just (re)packaging of the usage of
Object.prototype.toString.call(x), mechanism.
Without string literals, and without using 'typeof'.
And, as I said this implementation seems simple, rounded and portable
to me.  ( allow me to mention non-browser environments where this also
will work) .

--DBJ


On Jul 26, 12:09 am, Andrea Giammarchi <andrea.giammar...@gmail.com>
wrote:
> I miss the point about regexp usage ... please tell me the difference (in a
> real scenario) between these two checks:
>
> Object.prototype.toString.call(x) === Object.prototype.toString.call({})
>
> and
>
> Object.prototype.toString.call(x).match(/\w+/g)[1] ===
> Object.prototype.toString.call({}).match(/\w+/g)[1]
>
> finally please tell me the difference with this:
>
> Object.prototype.toString.call(x) === "[object Object]"
>
> I am usually against redundant, slow, or superflous code ... maybe I am
> wrong here, am I?
>
> On Sat, Jul 25, 2009 at 1:38 PM, DBJDBJ <dbj...@gmail.com> wrote:
>
> > This indeed woks :  function isObj(o){ return
> > Object.prototype.toString.call(o) === '[object Object]' }
>
> > But I do not like it ;o) It does not seem very exact somehow ... I
> > mean using a string literal, and all that...
> > I would suggest this very simple and robust little set of functions :
>
> > // and now much simpler implementation
> > function isWhat(o){ return Object.prototype.toString.call(o).match(/\w
> > +/g)[1]; }
>
> > function isObject(x) { return isWhat(x) === isWhat({}); }
> > function isArray(x)  { return isWhat(x) === isWhat([]); }
> > function isFunction(x) { return isWhat(x) === isWhat(Function()); }
> > function isString(x) { return isWhat(x) === isWhat(""); }
> > function isNumber(x) { return isWhat(x) === isWhat(1); }
>
> > No obfuscations here, and no "awesome" code. This seems very simple
> > and robust solution to me.
>
> > --DBJ
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"jQuery Development" group.
To post to this group, send email to jquery-dev@googlegroups.com
To unsubscribe from this group, send email to 
jquery-dev+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to