Usually type checking is very contextual like that. I rarely need a generic type checker, simply because I know that the thing I'm checking can only be a few things, so it's nice to simply test for array, for example. Also, not too many bytes are added, because what would be one function has been split up into 6-10. I think there is also a bigger reason at play. Libraries, like jQuery, are catering to developers who don't know a lot. These sorts of people are used to spending a lot of time in the documentation, they probably would be confused by a generic type-checker, because they understand they want to test for one thing, they may not even know what a "type" is. They just know they are expecting an object and want to test for it, a simple function that returns a boolean is easier to document and explain than a function that returns an assortment of strings depending on what is being passed. The MooTools private type checker returns up to 8 different things (testing for collections, and arguments, etc), newbie devs, or CSS-only guys just don't need to know this level of detail.
On Mon, May 16, 2011 at 1:15 PM, Jarek Foksa <[email protected]> wrote: > The built-in typeof operator has some major screwups: > > > typeof null > >> "object" > > > typeof [] > >> "object" > > > typeof /blah/ > >> "function" > > I think it would be nice to have two wrapper functions around typeof > that fix its inconsistencies: > - basetype() - that would return one of the 6 basic types as defined > in ECMAScript specification ('number', 'string', 'boolean', 'null', > 'undefined', 'object') > - type() - that besides 6 basic data types would additionally return > 'array', 'function', 'regExp' or 'element' > > After having investigated several implementations (most notably the > one from Crockford's "Remedial": > http://javascript.crockford.com/remedial.html) I have put together > this code: > > /** > * Base type check (returns either 'number', 'string', 'boolean', > 'null', 'undefined' or 'object') > */ > window.basetype = function(variable) { > var type = typeof variable; > > if (variable === null) { > return 'null'; > } > else if (type === 'function') { > return 'object'; > } > else { > return type; > } > } > > /** > * Type check (returns either 'number', 'string', 'boolean', 'null', > 'undefined', > * 'object', 'array', 'function', 'regExp' or 'element' > */ > window.type = function(variable) { > > var type = typeof variable; > > if (type === 'object') { > if (variable === null) { > return 'null'; > } > else if (toString.call(variable) === '[object Array]') { > return 'array'; > } > else if ( !!(variable && variable.nodeType === 1) ) { > return 'element'; > } > } > > if (type === 'function') { > if ( variable instanceof RegExp ) { > return 'regExp'; > } > } > > return type; > } > > My questions: > - Why so many frameworks are deciding to provide a set of checkers > such as isArray(), isNull(), isFunction() instead of single type() > function? Is performance gain really noticeable when using simple type > checkers? > - Are there any major mistakes that would hinder performance or > reliability of the functions above? Could they be improved any > further? > > -- > To view archived discussions from the original JSMentors Mailman list: > http://www.mail-archive.com/[email protected]/ > > To search via a non-Google archive, visit here: > http://www.mail-archive.com/[email protected]/ > > To unsubscribe from this group, send email to > [email protected] > -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/[email protected]/ To search via a non-Google archive, visit here: http://www.mail-archive.com/[email protected]/ To unsubscribe from this group, send email to [email protected]
