String object question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christopher Benson-Manica

    String object question


    <html>
    <head>
    <script>
    var s=String( 'foo' );
    alert( s );
    s.bar='bar';
    alert( s.bar );
    </script></head></html>

    Why does the second alert produce 'undefined'? Are string objects
    somehow special?

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
  • PDannyD

    #2
    Re: String object question

    Christopher Benson-Manica wrote:
    [color=blue]
    >
    > <html>
    > <head>
    > <script>
    > var s=String( 'foo' );
    > alert( s );
    > s.bar='bar';
    > alert( s.bar );
    > </script></head></html>
    >
    > Why does the second alert produce 'undefined'? Are string objects
    > somehow special?[/color]

    Shouldn't it be something like this?

    <html>
    <head>
    <script>
    var s=new String();
    s.first = "First string";
    alert(s.first);
    s.second = "Second string";
    alert(s.second) ;
    </script>
    </head>
    </html>

    Comment

    • Christopher Benson-Manica

      #3
      Re: String object question

      PDannyD <dan1970@scenic place.freeserve .co.uk> spoke thus:
      [color=blue]
      > Shouldn't it be something like this?[/color]
      [color=blue]
      > var s=new String();[/color]

      Yes, you're right. My way was trying to assign properties to a
      primitive string and of course you can't do that. Thank you.

      --
      Christopher Benson-Manica | I *should* know what I'm talking about - if I
      ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

      Comment

      • Lee

        #4
        Re: String object question

        Christopher Benson-Manica said:[color=blue]
        >
        >
        ><html>
        > <head>
        > <script>
        > var s=String( 'foo' );
        > alert( s );
        > s.bar='bar';
        > alert( s.bar );
        ></script></head></html>
        >
        >Why does the second alert produce 'undefined'? Are string objects
        >somehow special?[/color]

        You didn't create a String object, just a simple string.
        Try it with:

        var s = new String( 'foo' );

        Comment

        • Christopher Benson-Manica

          #5
          Re: String object question

          Lee <REM0VElbspamtr ap@cox.net> spoke thus:
          [color=blue]
          > You didn't create a String object, just a simple string.
          > Try it with:[/color]
          [color=blue]
          > var s = new String( 'foo' );[/color]

          Yes, that works. Here's a followup question though: Does the split()
          method produce an array of string objects or primitive strings?

          --
          Christopher Benson-Manica | I *should* know what I'm talking about - if I
          ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

          Comment

          • Lee

            #6
            Re: String object question

            Christopher Benson-Manica said:[color=blue]
            >
            >Lee <REM0VElbspamtr ap@cox.net> spoke thus:
            >[color=green]
            >> You didn't create a String object, just a simple string.
            >> Try it with:[/color]
            >[color=green]
            >> var s = new String( 'foo' );[/color]
            >
            >Yes, that works. Here's a followup question though: Does the split()
            >method produce an array of string objects or primitive strings?[/color]

            They are primitive strings.
            The following alerts "string", rather than "object":

            <html>
            <body>
            <script type="text/javascript">
            var token="alpha,be ta,gamma".split (",");
            alert(typeof token[0]);
            </script>
            </body>
            </html>

            Comment

            • RobB

              #7
              Re: String object question

              Christopher Benson-Manica wrote:[color=blue]
              > Lee <REM0VElbspamtr ap@cox.net> spoke thus:
              >[color=green]
              > > You didn't create a String object, just a simple string.
              > > Try it with:[/color]
              >[color=green]
              > > var s = new String( 'foo' );[/color]
              >
              > Yes, that works. Here's a followup question though: Does the split()
              > method produce an array of string objects or primitive strings?
              >
              > --
              > Christopher Benson-Manica | I *should* know what I'm talking about -[/color]
              if I[color=blue]
              > ataru(at)cybers pace.org | don't, I need to know. Flames welcome.[/color]

              This may help:



              Comment

              Working...