不懂急,请问javascript的tostring()方法问题?
<script language="javascript">
var myobject1=new object();
myobject1.name="铅笔";
myobject1.price=20;
var myobject2=new object(1.1258933);
var myobject3=new object(false);
var myobject4=new object("str");
document.write(myobject1.tostring()+"<br>");返回[object object]
document.write(myobject2.tostring()+"<br>");返回[1.1258933]
document.write(myobject3.tostring()+"<br>");返回[false]
document.write(myobject4.tostring()+"<br>");返回[str]
</script>tostring()方法通常在调试javascript代码时使用,使用tostring()方法可以输出对象,查看对象的值不同类型的对象输出的字符串都是不同的
-------------------------------------------------
第一个问题:tostring() 怎么输出对象?输出的应该是字符串,怎么会是对象?第二个问题:在object对象中使用toString()方法将会返回类似于[object object]字符串,这个object对象是不是引用后的对象,如myobject1,然后myobject1.tostring()返回的是[object object],为什么是两个object,我不懂,object()函数没有参数,为什么myobject1.tostring返回是[object object]是什么意思啊?不懂啊.
第三个问题:在object对象中使用tostring()方法将会返回类似于[object class]的字符串,其中class的可能值为object,string,number,function,document和window等[object class]这个完全不懂,什么情况下会返回这个,为什么会返回object和class两个?[object对象 class类]什么情况下,对象使用了tostring()会返回[object对象 class类]是什么原因返回出object对象,是什么原因返回出class类,又是什么原因返回出[object对象 class类]
第四个问题:在什么情况下返回出[object object],[object string],[object,number]
[object,function],[object,document]
请依依举例说明.
第五个问题:如果自定义对象中没有创建tostring()方法,会继承object对象的tostring方法,返回[object object]自定义对象的class永远都是object
什么叫作自定义对象中没有创建tostring()方法,会继承object对象的tostring方法,什么意思?请举例说明.完全不懂,急急急,啥意思啊?急急急.自定义对象的class永远都是object,什么意思?new object()这个object()是个函数,然后用new创建一个对象,给myobject1引用,myobject1是个对象,哪有什么class,哪冒出来的啊?class永远都是object就更迷惑了,急急急,急啊,急急急急..................

解决方案 »

  1.   


    Built-in toString methods
    Every object type has a built-in toString method, which JavaScript calls whenever it needs to convert an object to a string. If an object has no string value and no user-defined toString method, toString returns "[object type]", where type is the object type or the name of the constructor function that created the object. For example, if for an Image object named sealife defined as shown below, sealife.toString() returns [object Image].
    <IMG NAME="sealife" SRC="images\seaotter.gif" ALIGN="left" VSPACE="10"> Some built-in classes have special definitions for their toString methods. See the描述s of this method for these objects:Array, Boolean, Connection, database, DbPool, Function, Number User-defined toString methods
    You can create a function to be called in place of the default toString method. The toString method takes no arguments and should return a string. The toString method you create can be any value you want, but it will be most useful if it carries information about the object.
    The following code defines the Dog object type and creates theDog, an object of type Dog:function Dog(name,breed,color,sex) {
       this.name=name
       this.breed=breed
       this.color=color
       this.sex=sex
    } theDog = new Dog("Gabby","Lab","chocolate","girl") The following code creates dogToString, the function that will be used in place of the default toString method. This function generates a string containing each property, of the form "property = value;".function dogToString() {
       var ret = "Dog " + this.name + " is ["
       for (var prop in this)
          ret += " " + prop + " is " + this[prop] + ";"
       return ret + "]"
    } The following code assigns the user-defined function to the object's toString method:Dog.prototype.toString = dogToString With the preceding code in place, any time theDog is used in a string context, JavaScript automatically calls the dogToString function, which returns the following string:
    Dog Gabby is [ name is Gabby; breed is Lab; color is chocolate; sex is girl; toString is function dogToString() { var ret = "Object " + this.name + " is ["; for (var prop in this) { ret += " " + prop + " is " + this[prop] + ";"; } return ret + "]"; } ;] 
    An object's toString method is usually invoked by JavaScript, but you can invoke it yourself as follows:
    alert(theDog.toString()) 示例
    示例 1: The location object. The following example prints the string equivalent of the current location.
    document.write("location.toString() is " + location.toString() + "<BR>") The output is as follows:location.toString() is file:///C|/TEMP/myprog.html 示例 2: Object with no string value. Assume you have an Image object named sealife defined as follows:<IMG NAME="sealife" SRC="images\seaotter.gif" ALIGN="left" VSPACE="10"> Because the Image object itself has no special toString method, sealife.toString() returns the following:[object Image] 示例 3: The radix parameter. The following example prints the string equivalents of the numbers 0 through 9 in decimal and binary.for (x = 0; x < 10; x++) {
       document.write("Decimal: ", x.toString(10), " Binary: ",
          x.toString(2), "<BR>")
    } The preceding example produces the following output:Decimal: 0 Binary: 0
    Decimal: 1 Binary: 1
    Decimal: 2 Binary: 10
    Decimal: 3 Binary: 11
    Decimal: 4 Binary: 100
    Decimal: 5 Binary: 101
    Decimal: 6 Binary: 110
    Decimal: 7 Binary: 111
    Decimal: 8 Binary: 1000
    Decimal: 9 Binary: 1001 
      

  2.   

    Javascript高级程序设计解释:
    toString()--返回对象的原始字符串表示。对于Object类,ECMA-262没有定义这个值,所以不同的ECMAScript实现具有不同的值。
    而且Object类本身用处不大,toString方法会被其他类覆盖。
    经我测试,在IE下返回的是Object原型对象(即prototype属性)。
      

  3.   

    所有对象都是Object类的实例或子类
    toString的一个方法,该方法由子类实现如果LZ要自己模拟创建类可以这样写klass.prototype.constructor = klass;
    这样创建出来的实例返回的类型就不是object了建议LZ把js的基础好好的学习一边,这类问题就自然会明白了