javascript - When is it safe to use .toString()? - Stack Overflow

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

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>

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>
Share Improve this question edited Jun 27, 2009 at 0:30 Ben Blank 56.7k28 gold badges132 silver badges163 bronze badges asked Jun 26, 2009 at 19:48 Delirium tremensDelirium tremens 4,7499 gold badges47 silver badges59 bronze badges 4
  • Real questions have a verb and a question mark – Eric Commented Jun 26, 2009 at 19:53
  • I said "Doesn't value have to return toString() to be able to call value.toString()? When do you know you can call value.toString()?". – Delirium tremens Commented Jun 26, 2009 at 20:02
  • 1 This question really doesn't have anything to do with linked lists. – Mr. Shiny and New 安宇 Commented Jun 26, 2009 at 20:05
  • Maybe you could give a more meaningful heading than "linked list" so that it could be of use to others – pugmarx Commented Jun 27, 2009 at 0:00
Add a ment  | 

4 Answers 4

Reset to default 5

Every object in JavaScript has a toString() method.

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!");
}

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

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742287243a4415540.html

相关推荐

  • javascript - When is it safe to use .toString()? - Stack Overflow

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

    16小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信