Is it necessary for the value to return toString() in order to call value.toString()? How can you determine when you are able to call value.toString()?
<script>
var customList = 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 = customList(val, null);
else
this.tail.append(val);
}
};
}
var myList = customList("abc", null); // a string
myList.append(3.14); // a floating-point number
myList.append([1, 2, 3]); // an array
document.write(myList.toString());
</script>