On Wednesday 11 February 2009 05:06:55 RobG wrote:
> The language is designed so you shouldn't need to care.  I think this
> helps the argument of why functions like isString are not a good
> idea.  If you want to test for specific properties or features of an
> object, test for them.  Don't test some other property and make
> assumptions based on it, which is what isString encourages.

With Strings, It is probably *always* the correct way to look at the 
constructor, ie if( foo.constructor == String ) - this tells me if it is a 
string (obejct or literal) without me having to care how the string was 
created.

I can't think of any time when I care if it is an object or literal, so IMO 
for strings looking at the constructor is *just*better* than asking typeof. If 
we're going to have isString at all it might as well check using the more 
useful constructor way rather than the less useful typeof way.

> Much better to learn the ins and outs of typeof and other operators so
> you know to use them appropriately.  It doesn't make sense to me to
> paper over such things as they will come back to haunt you in
> unexpected ways.

I think Object.isString(s) as a replacement for (s.constructor == String) 
makes the code a little easier to read, especially when used alongside other 
type checks. For example, I might have:

if( Object.isString( foo ) ) {
    /* ... */
}elseif( Object.isObject( foo ) ){
    /* ... */
}

which (I think) is nicer than this:

if( foo.constructor == String ) {
    /* ... */
}elseif( typeof foo == 'object' ){
    /* ... */
}

Of course this is purely subjective but I find the repetition makes it more 
obvious at first glance that we're checking for type both times.

[snip]

> Incidentally, Prototype.js uses isString in 12 places, so your
> suggested mod has some consequences.

Then presumably 12 of places in Prototype don't think String objects exist and 
would break if passed them. I've never seen the API specify literal strings so 
here's the bug :-)

If it has been fixed in trunc (I haven't checked, but it was said to have been 
in this thread), presumably the 12 places didn't cause regressions.

-- 
Jim
blog: http://jimhigson.blogspot.com/
web: http://wikizzle.org

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to