2

Doesn't value have to return toString() to be able to call value.toString()? When do you know you can call value.toString()?

<script>
var newList = function(val, lst)
{
  return {
    value: val,
    tail:  lst,
    toString: function() 
    {
      var result = this.value.toString();
      if (this.tail != null)
        result += "; " + this.tail.toString();
      return result;
    },
    append: function(val)
    {
      if (this.tail == null)
        this.tail = newList(val, null);
      else
        this.tail.append(val);
    }
  };
}

var list = newList("abc", null); // a string
list.append(3.14); // a floating-point number
list.append([1, 2, 3]); // an array
document.write(list.toString());
</script>
Ben Blank
  • 54,908
  • 28
  • 127
  • 156
Delirium tremens
  • 4,623
  • 9
  • 46
  • 59

4 Answers4

5

Every object in JavaScript has a toString() method.

Mr. Shiny and New 安宇
  • 13,822
  • 6
  • 44
  • 64
  • 1
    No, not all objects. null has no properties. Also undefined has no properties, though it is not an object. https://jsfiddle.net/bca30e5h/ – Aurimas Jun 04 '15 at 05:10
  • @Aurimas `null` is not really an object. The JS spec is confused about this. Null pointers _lack_ an object. See [this](http://stackoverflow.com/a/18808300/7867) and other answers to that question. – Mr. Shiny and New 安宇 Jun 04 '15 at 13:16
  • 2
    That's interesting. Thanks for correcting me. In any case, it's still worth pointing out that there are cases when toString is not safe to use. – Aurimas Jun 04 '15 at 13:22
  • simple solution is `if (val) result = val.toString()` – Qamar Stationwala Oct 03 '22 at 20:19
5

As Mr. Shiny and New states, all JavaScript objects have a toString method. However, that method is not always useful, especially for custom classes and object literals, which tend to return strings like "[Object object]".

You can create your own toString methods by adding a function with that name to your class' prototype, like so:

function List(val, list) {
    this.val = val;
    this.list = list;

    // ...
}

List.prototype = {
    toString: function() {
        return "newList(" + this.val + ", " + this.list + ")";
    }
};

Now, if you create a new List(...) and call its toString method (or run it through any function or operator that converts it to a string implicitly), your custom toString method will be used.

Finally, to detect whether an object has a toString method defined for its class (note that this will not work with subclassing or object literals; that is left as an exercise for the reader), you can access its constructor's prototype property:

if (value.constructor.prototype.hasOwnProperty("toString")) {
    alert("Value has a custom toString!");
}
Community
  • 1
  • 1
Ben Blank
  • 54,908
  • 28
  • 127
  • 156
  • Heh. I do that all the time in Real Life, too. I guess that's what I get for working in too many different languages. :-) – Ben Blank Jun 29 '09 at 19:57
0

document.write, like window.alert, calls its argument's toString method before it writes or returns anything.

kennebec
  • 102,654
  • 32
  • 106
  • 127
0

Other answers are correct that toString exists on all Javascript objects.

In general, though, if you want to know if a function exists on your object, you can test it like so:

if (obj.myMethod) {
    obj.myMethod();
}

This does not, of course, ensure that myMethod is a function instead of a property. But presumably you'll know that.

Gabe Moothart
  • 31,211
  • 14
  • 77
  • 99