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

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

Expand Messages
  • Stefan Weiss
    Feb 17, 2010
    • 0 Attachment
      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.

    • Show all 15 messages in this topic