📄 object.html
字号:
this.name=name<br> this.breed=breed<br> this.color=color<br> this.sex=sex<br>}<br>theDog = new Dog("Gabby","Lab","chocolate","girl")</zcodeline></FONT><P><A NAME="1204667"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Calling the <FONT FACE="courier, couriernew, monospaced size">toSource</FONT> method of <FONT FACE="courier, couriernew, monospaced size">theDog</FONT> displays the JavaScript source that defines the object:</zparagraph></FONT><P><A NAME="1204789"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="-1"><zcodeline>theDog.toSource()<br>//returns "{name:"Gabby", breed:"Lab", color:"chocolate", sex:"girl"}</zcodeline></FONT><P><BR><A NAME="1193343"> </A><A NAME="See also"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><B><zrefheader>See also</zrefheader></B></FONT><BR><A NAME="1193347"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph><A HREF="object.html#1193350" >Object.toString</A></zparagraph></FONT><P><BR><P><A NAME="1193350"> </A><A NAME="toString"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="4"><B><zrefhead2>toString</zrefhead2></B></FONT><P><A NAME="1193363"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Returns a string representing the specified object.<BR><P><TABLE WIDTH="90%" BORDER="1" CELLPADDING="5" CELLSPACING="0"> <CAPTION></CAPTION><P><FONT FACE=""><B></B></FONT><BR><BR> <TR> <TD VALIGN=TOP><P><A NAME="1193353"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><ztabletext><I>Method of</I></ztabletext></FONT> </TD> <TD VALIGN=TOP><P><A NAME="1193358"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><ztabletext><A HREF="object.html#1193136" >Object</A></ztabletext></FONT> </TD> </TR> <TR> <TD VALIGN=TOP><P><A NAME="1193360"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><ztabletext><I>Implemented in</I></ztabletext></FONT> </TD> <TD VALIGN=TOP><P><A NAME="1193362"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><ztabletext>JavaScript 1.0</ztabletext></FONT> </TD> </TR> <TR> <TD VALIGN=TOP><P><A NAME="1196496"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><ztabletext><I>ECMA version</I></ztabletext></FONT> </TD> <TD VALIGN=TOP><P><A NAME="1196498"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><ztabletext>ECMA-262</ztabletext></FONT> </TD> </TR></TABLE><P></zparagraph></FONT><P><BR><A NAME="1203456"> </A><A NAME="Syntax"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><B><zrefheader>Syntax</zrefheader></B></FONT><BR><A NAME="1203457"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="-1"><zcodeline>toString()</zcodeline></FONT><P><BR><A NAME="1193421"> </A><A NAME="Description"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><B><zrefheader>Description</zrefheader></B></FONT><BR><A NAME="1193422"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Every object has a <FONT FACE="courier, couriernew, monospaced size">toString</FONT> method that is automatically called when it is to be represented as a text value or when an object is referred to in a string concatenation. For example, the following examples require <FONT FACE="courier, couriernew, monospaced size">theDog</FONT> to be represented as a string:</zparagraph></FONT><P><A NAME="1193423"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="-1"><zcodeline>document.write(theDog)<br>document.write("The dog is " + theDog)</zcodeline></FONT><P><A NAME="1200394"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>By default, the <FONT FACE="courier, couriernew, monospaced size">toString</FONT> method is inherited by every object descended from <FONT FACE="courier, couriernew, monospaced size">Object</FONT>. You can override this method for custom objects that you create. If you do not override <FONT FACE="courier, couriernew, monospaced size">toString</FONT> in a custom object, <FONT FACE="courier, couriernew, monospaced size">toString</FONT> returns <FONT FACE="courier, couriernew, monospaced size">[object </FONT><FONT FACE="courier, couriernew, monospaced size"><I>type</I></font><FONT FACE="courier, couriernew, monospaced size">]</FONT>, where <FONT FACE="courier, couriernew, monospaced size"><I>type</I></font> is the object type or the name of the constructor function that created the object.</zparagraph></FONT><P><A NAME="1203849"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>For example:</zparagraph></FONT><P><A NAME="1203854"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="-1"><zcodeline>var o = new Object()<br>o.toString // returns [object Object]</zcodeline></FONT><P><P><A NAME="1193426"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><B><zheadrunin>Built-in toString methods. </zheadrunin></B></FONT><A NAME="1203699"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Every built-in core JavaScript object overrides the <FONT FACE="courier, couriernew, monospaced size">toString</FONT> method of <FONT FACE="courier, couriernew, monospaced size">Object</FONT> to return an appropriate value. JavaScript calls this method whenever it needs to convert an object to a string.</zparagraph></FONT><P><P><A NAME="1193453"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><B><zheadrunin>Overriding the default toString method. </zheadrunin></B></FONT><A NAME="1193454"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You can create a function to be called in place of the default <FONT FACE="courier, couriernew, monospaced size">toString</FONT> method. The <FONT FACE="courier, couriernew, monospaced size">toString</FONT> method takes no arguments and should return a string. The <FONT FACE="courier, couriernew, monospaced size">toString</FONT> method you create can be any value you want, but it will be most useful if it carries information about the object.</zparagraph></FONT><P><A NAME="1193455"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The following code defines the <FONT FACE="courier, couriernew, monospaced size">Dog</FONT> object type and creates <FONT FACE="courier, couriernew, monospaced size">theDog,</FONT> an object of type <FONT FACE="courier, couriernew, monospaced size">Dog</FONT>:</zparagraph></FONT><P><A NAME="1193456"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="-1"><zcodeline>function Dog(name,breed,color,sex) {<br> this.name=name<br> this.breed=breed<br> this.color=color<br> this.sex=sex<br>}</zcodeline></FONT><P><A NAME="1193457"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="-1"><zcodeline>theDog = new Dog("Gabby","Lab","chocolate","girl")</zcodeline></FONT><P><A NAME="1193458"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>If you call the <FONT FACE="courier, couriernew, monospaced size">toString</FONT> method on this custom object, it returns the default value inherited from <FONT FACE="courier, couriernew, monospaced size">Object</FONT>:</zparagraph></FONT><P><A NAME="1203749"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="-1"><zcodeline>theDog.toString() //returns [object Object]</zcodeline></FONT><P><A NAME="1203735"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The following code creates <FONT FACE="courier, couriernew, monospaced size">dogToString,</FONT> the function that will be used to override the default <FONT FACE="courier, couriernew, monospaced size">toString</FONT> method. This function generates a string containing each property, of the form <FONT FACE="courier, couriernew, monospaced size">"property = value;"</FONT>.</zparagraph></FONT><P><A NAME="1193459"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="-1"><zcodeline>function dogToString() {<br> var ret = "Dog " + this.name + " is [\n"<br> for (var prop in this)<br> ret += " " + prop + " is " + this[prop] + ";\n"<br> return ret + "]"<br>}</zcodeline></FONT><P><A NAME="1193460"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The following code assigns the user-defined function to the object's <FONT FACE="courier, couriernew, monospaced size">toString</FONT> method:</zparagraph></FONT><P><A NAME="1193461"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="-1"><zcodeline>Dog.prototype.toString = dogToString</zcodeline></FONT><P><A NAME="1193462"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>With the preceding code in place, any time <FONT FACE="courier, couriernew, monospaced size">theDog</FONT> is used in a string context, JavaScript automatically calls the <FONT FACE="courier, couriernew, monospaced size">dogToString</FONT> function, which returns the following string:</zparagraph></FONT><P><A NAME="1203804"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="-1"><zcodeline>Dog Gabby is [<br> name is Gabby;<br> breed is Lab;<br> color is chocolate;<br> sex is girl;<br>]</zcodeline></FONT><P><A NAME="1203805"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>An object's <FONT FACE="courier, couriernew, monospaced size">toString</FONT> method is usually invoked by JavaScript, but you can invoke it yourself as follows:</zparagraph></FONT><P><A NAME="1193465"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="-1"><zcodeline>var dogString = theDog.toString()</zcodeline></FONT><P><BR><A NAME="1193466"> </A><A NAME="Backward Compatibility"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><B><zrefheader>Backward Compatibility</zrefheader></B></FONT><BR><P><A NAME="1193467"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><B><zheadrunin>JavaScript 1.2. </zheadrunin></B></FONT><A NAME="1193468"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The behavior of the <FONT FACE="courier, couriernew, monospaced size">toString</FONT> method depends on whether you specify <FONT FACE="courier, couriernew, monospaced size">LANGUAGE="JavaScript1.2"</FONT> in the <FONT FACE="courier, couriernew, monospaced size"><SCRIPT></FONT> tag:</zparagraph></FONT><P><ul><A NAME="1193469"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><B> <li></B><zsmartlist1>If you specify <FONT FACE="courier, couriernew, monospaced size">LANGUAGE="JavaScript1.2"</FONT> in the <FONT FACE="courier, couriernew, monospaced size"><SCRIPT></FONT> tag, the <FONT FACE="courier, couriernew, monospaced size">toString</FONT> method returns an object literal.</zsmartlist1></FONT><P><A NAME="1193470"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><B> <li></B><zsmartlist1>If you do not specify <FONT FACE="courier, couriernew, monospaced size">LANGUAGE="JavaScript1.2"</FONT> in the <FONT FACE="courier, couriernew, monospaced size"><SCRIPT></FONT> tag, the <FONT FACE="courier, couriernew, monospaced size">toString</FONT> method returns <FONT FACE="courier, couriernew, monospaced size">[object </FONT><FONT FACE="courier, couriernew, monospaced size"><I>type</I></font><FONT FACE="courier, couriernew, monospaced size">]</FONT>, as with other JavaScript versions.</zsmartlist1></FONT><P></ul><BR><A NAME="1193471"> </A><A NAME="Examples"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><B><zrefheader>Examples</zrefheader></B></FONT><BR><A NAME="1193472"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph><B>Example 1: The location object.</B> The following example prints the string equivalent of the current location.</zparagraph></FONT><P><A NAME="1193473"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="-1"><zcodeline>document.write("location.toString() is " + location.toString() + "<BR>")</zcodeline></FONT><P><A NAME="1193474"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The output is as follows:</zparagraph></FONT><P><A NAME="1195036"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="-1"><zcodeline>location.toString() is file:///C|/TEMP/myprog.htmll</zcodeline></FONT><P><A NAME="1195040"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph><B>Example 2: Object with no string value.</B> Assume you have an <FONT FACE="courier, couriernew, monospaced size">Image</FONT> object named <FONT FACE="courier, couriernew, monospaced size">sealife</FONT> defined as follows:</zparagraph></FONT><P><A NAME="1193480"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="-1"><zcodeline><IMG NAME="sealife" SRC="images\seaotter.gif" ALIGN="left" VSPACE="10"></zcodeline></FONT><P><A NAME="1193484"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Because the <FONT FACE="courier, couriernew, monospaced size">Image</FONT> object itself has no special <FONT FACE="courier, couriernew, monospaced size">toString</FONT> method, <FONT FACE="courier, couriernew, monospaced size">sealife.toString()</FONT> returns the following:</zparagraph></FONT><P><A NAME="1193485"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="-1"><zcodeline>[object Image]</zcodeline></FONT><P><A NAME="1193486"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph><B>Example 3: The radix parameter.</B> The following example prints the string equivalents of the numbers 0 through 9 in decimal and binary.</zparagraph></FONT><P><A NAME="1193487"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="-1"><zcodeline>for (x = 0; x < 10; x++) {<br> document.write("Decimal: ", x.toString(10), " Binary: ",<br> x.toString(2), "<BR>")<br>}</zcodeline></FONT><P><A NAME="1193488"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The preceding example produces the following output:</zparagraph></FONT><P><A NAME="1193489"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="-1"><zcodeline>Decimal: 0 Binary: 0<br>Decimal: 1 Binary: 1<br>Decimal: 2 Binary: 10<br>Decimal: 3 Binary: 11<br>Decimal: 4 Binary: 100<br>Decimal: 5 Binary: 101<br>Decimal: 6 Binary: 110<br>Decimal: 7 Binary: 111<br>Decimal: 8 Binary: 1000<br>Decimal: 9 Binary: 1001</zcodeline></FONT><P><BR><A NAME="1193490"> </A><A NAME="See also"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><B><zrefheader>See also</zrefheader></B></FONT><BR><A NAME="1193497"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph><A HREF="object.html#1193320" >Object.toSource</A>, <A HREF="object.html#1193540" >Object.valueOf</A></zparagraph></FONT><P><BR><P><A NAME="1193499"> </A><A NAME="unwatch"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="4"><B><zrefhead2>unwatch</zrefhead2></B></FONT><P><A NAME="1193516"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Removes a watchpoint set with the <A HREF="object.html#1193628" >watch</A> method.<BR><P><TABLE WIDTH="90%" BORDER="1" CELLPADDING="5" CELLSPACING="0"> <CAPTION></CAPTION><P><FONT FACE=""><B></B></FONT><BR><BR> <TR> <TD VALIGN=TOP><P><A NAME="1193506"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><ztabletext><I>Method of</I></ztabletext></FONT> </TD> <TD VALIGN=TOP><P><A NAME="1193511"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><ztabletext><A HREF="object.html#1193136" >Object</A></ztabletext></FONT> </TD> </TR> <TR> <TD VALIGN=TOP><P><A NAME="1193513"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><ztabletext><I>Implemented in</I></ztabletext></FONT> </TD> <TD VALIGN=TOP><P><A NAME="1193515"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><ztabletext>JavaScript 1.2, NES 3.0</ztabletext></FONT> </TD> </TR></TABLE><P></zparagraph></FONT><P><BR><A NAME="1193517"> </A><A NAME="Syntax"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><B><zrefheader>Syntax</zrefheader></B></FONT><BR><A NAME="1193518"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="-1"><zcodeline>unwatch(<I>prop</I>)</zcodeline></FONT><P><BR><A NAME="1193519"> </A><A NAME="Parameters"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><B><zrefheader>Parameters</zrefheader></B></FONT><BR><A NAME="1193525"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph><BR><P><TABLE WIDTH="90%" BORDER="1" CELLPADDING="5" CELLSPACING="0"> <CAPTION></CAPTION><P><FONT FACE=""><B></B></FONT><BR><BR> <TR> <TD VALIGN=TOP><P><A NAME="1193522"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><ztabletext><FONT FACE="courier, couriernew, monospaced size">prop</FONT></ztabletext></FONT> </TD> <TD VALIGN=TOP><P><A NAME="1193524"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="-1"><ztabletext>The name of a property of the object.</ztabletext></FONT> </TD>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -