On Feb 7, 9:55 am, Jim Higson <[email protected]> wrote:
> alert( Object.isString( "foo" ));
> // alerts "string"
> alert( Object.isString( new String( "foo" ) ));
> // alerts "object"
>
> I know why this happens - because Prototype uses the underlying typeof's
> understanding of type.
>
> Personally I think this is a bug though - Javascript is just being difficult
> here and Prototype should make stuff like this easier. It should
> return "string" regardless of how the string was created.
>
> Anyone agree I should file this as a bug, or is there a good reason the
> current behaviour is desirable?

Javascript is actually quite flexible in this regard. If you want to
check whether an object is a string *primitive* - use `typeof` and
compare result to "string". If you want to know whether an object is
an instance of `String` function and inherits from `String.prototype`,
use `instanceof` operator or check object's `constructor` property. If
you need to use any of `String.prototype.*` methods, explicitly check
for a presence of those methods on an object (since property lookup
would propagate up the prototype chain) - e.g. `if (typeof
object.charCodeAt == 'function') { ... }`; Finally, you can always
check object's internal [[Class]] for a value of "String" (which all
*native* String *objects* have). In a trunk of prototype.js, we use
this last method - Object.prototype.toString.call(object) === '[object
String]', where `call` first converts a value to an object and
`Object.prototype.toString` then checks that object's [[Class]] value;
It works for both - primitives and their object representations.

--
kangax
--~--~---------~--~----~------------~-------~--~----~
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