convert object to string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Max

    convert object to string

    I just wonder if there is a way of converting an object to a string.
    I am using a function to send an object parameter e.g "fctng(train0); "
    later on I would like to use train0 but as a string to access a
    img id called <img id="train0"> .
    This might be obvious to you but I am not a programmer, just trying to
    learn javascript at home for fun.
    Thanks

    Max
  • web.dev

    #2
    Re: convert object to string

    Hi Max,
    [color=blue][color=green]
    >> I just wonder if there is a way of converting an object to a string.[/color][/color]

    Yes and no. It depends on the situation. Are you wanting to convert a
    javascript object or a user-defined object? All javascript objects
    have a toString method, however if it's an object that you've made
    yourself, you'll need to provide a way to convert it to a string.

    Comment

    • Richard Cornford

      #3
      Re: convert object to string

      Max wrote:[color=blue]
      > I just wonder if there is a way of converting an object
      > to a string.[/color]

      In loosely typed javascript any type can be converted into any other
      type, and they are automatically internally converted to resolve
      expressions whenever the context of use requires a different type. See:-

      <URL: http://www.jibbering.com/faq/faq_not...e_convert.html >


      The rule for converting Objects to strings is that the object's -
      toString - method is called and the result is the string primitive value
      of the object. Objects inherit default - toString - methods from their
      prototype. For objects created with the object constructor, constructor
      functions and object literals that - toString - function is the one
      defined on the - Object.prototyp e -, which is not particularly useful as
      it is specified as returning "[object Object]".

      You get around the default - toString - methods of objects by providing
      your own. EG:-

      function MyObject(type, value){
      this.type = type;
      this.value = value;
      }
      MyObject.protot ype.toString = function(){
      return (
      "[MyObject: \n\ttype = "+
      this.type+
      "\n\tvalue = "+
      this.value+
      "\n]\n"
      );
      };

      - giving an object that when type-converted to a string (or as a result
      of having its toString method called directly) will return a string
      describing the object's current state. I.E.:-

      var obj = new MyObject('somet ype', 55);

      var str = ''+obj; //concatenation forces obj's type-conversion to a
      string.

      alert(str); // --> outputs:-

      [MyObject:
      type = sometype
      value = 55
      ]

      - in the alert box.

      The same result will follow from calling the object's - toString -
      method directly as:-

      obj.toString();
      [color=blue]
      > I am using a function to send an object parameter e.g
      > "fctng(train0); " later on I would like to use train0 but
      > as a string to access a img id called <img id="train0"> .[/color]

      That is not a sufficient explanation of what you are attempting (or why)
      to convey meaning. Remember, code always contributes to understanding
      when you are talking about scripting.
      [color=blue]
      > This might be obvious to you but I am not a programmer,
      > just trying to learn javascript at home for fun.[/color]

      No harm in that. Look at the information that the FAQ provides on the
      subject of asking questions, it will help in the long run.

      Richard.


      Comment

      Working...