Loading ...
Sorry, an error occurred while loading the content.

1158Re: [jslint] I must use 'new String'

Expand Messages
  • Morgaut Alexandre Louis Marc
    Feb 17, 2010
    • 0 Attachment
      For the exposed use case I wouldn't recommend this kind of weird
      custom objects wich wouldn't have any string methods and wouldn't be
      seen as string by typeof

      My suggestion would be to use an external object for additional
      properties like this :

      var strProps = {};

      var str = "something";

      strProps[str] = {
      id: 123
      };

      You can either use a namespaced strProps (like myApp.strProps) or a
      local one for more privacy depending of your requirements

      Alexandre



      Le 18 févr. 10 à 06:30, Stefan Weiss a écrit :

      > On 17/02/10 14:11, Klemen Slavič wrote:
      > > Why not use JSON to carry values in such a case?
      > >
      > > var a = {
      > > value: "somestring",
      > > id: 123
      > > };
      >
      > That's not JSON, it's just an object literal.
      >
      > As to the previous example:
      >
      > var str = "somestring";
      > str.id = 123;
      > str.id; // undefined
      >
      > The reason why this can't work is that in lines 2 and 3, str is
      > converted from a string primitive to a new String object. This happens
      > every time you try to use a string (or number, or boolean) primitive
      > type like an object --> str gets auto-converted into an object twice,
      > but only internally, for the purpose of property lookup. The two
      > created
      > String objects are then thrown away, and the "id" property is lost.
      >
      > For the OP: I agree with Klemen and the walfisch - wrapping your
      > string
      > and any additional properties in a custom object is the way to go.
      > Augmenting one instance of a String object sounds like flaky design; I
      > sure wouldn't expect it if I read your code.
      >
      > --
      > stefan
      >
      >



      [Non-text portions of this message have been removed]
    • Show all 15 messages in this topic