⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 function.html

📁 javascript 1.5规范
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<A NAME="1203759"> </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 BGCOLOR="#CCCCCC" ALIGN=LEFT VALIGN=TOP>    <TH VALIGN=TOP ALIGN=LEFT><A NAME="1203712"> </A><P ALIGN="LEFT"><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><B><ztablehead>Method</ztablehead></B></FONT></TH>    <TH VALIGN=TOP ALIGN=LEFT><A NAME="1203714"> </A><P ALIGN="LEFT"><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><B><ztablehead>Description</ztablehead></B></FONT></TH>  </DIV></TR>  <TR>    <TD VALIGN=TOP><A NAME="1203719"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline><A HREF="function.html#1194017"  >apply</A></zcodeline></FONT><P>&nbsp;</TD>    <TD VALIGN=TOP><P><A NAME="1203721"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext>Allows you to apply a method of another object in the context of a different object (the calling object).</ztabletext></FONT>&nbsp;</TD>  </TR>  <TR>    <TD VALIGN=TOP><A NAME="1203726"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline><A HREF="function.html#1194243"  >call</A></zcodeline></FONT><P>&nbsp;</TD>    <TD VALIGN=TOP><P><A NAME="1203728"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext>Allows you to call (execute) a method of another object in the context of a different object (the calling object).</ztabletext></FONT>&nbsp;</TD>  </TR>  <TR>    <TD VALIGN=TOP><A NAME="1203733"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline><A HREF="function.html#1193583"  >toSource</A></zcodeline></FONT><P>&nbsp;</TD>    <TD VALIGN=TOP><P><A NAME="1203735"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext>Returns a string representing the source code of the function. Overrides the <A HREF="object.html#1193320"  >Object.toSource</A> method.</ztabletext></FONT>&nbsp;</TD>  </TR>  <TR>    <TD VALIGN=TOP><A NAME="1203743"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline><A HREF="function.html#1193615"  >toString</A></zcodeline></FONT><P>&nbsp;</TD>    <TD VALIGN=TOP><P><A NAME="1203745"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext>Returns a string representing the source code of the function. Overrides the <A HREF="object.html#1193350"  >Object.toString</A> method.</ztabletext></FONT>&nbsp;</TD>  </TR>  <TR>    <TD VALIGN=TOP><A NAME="1203753"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline><A HREF="function.html#1199980"  >valueOf</A></zcodeline></FONT><P>&nbsp;</TD>    <TD VALIGN=TOP><P><A NAME="1203755"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext>Returns a string representing the source code of the function. Overrides the <A HREF="object.html#1193540"  >Object.valueOf</A> method.</ztabletext></FONT>&nbsp;</TD>  </TR></TABLE><P></zparagraph></FONT><P><BR><A NAME="1220053"> </A><A NAME="Examples"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><B><zrefheader>Examples</zrefheader></B></FONT><BR><A NAME="1220635"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph><B>Example 1.</B> The following function returns a string containing the formatted representation of a number padded with leading zeros.</zparagraph></FONT><P><A NAME="1227379"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline>// This function returns a string padded with leading zeros<br>function padZeros(num, totalLen) {<br>&nbsp;&nbsp;&nbsp;var numStr = num.toString()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Initialize return value<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// as string<br>&nbsp;&nbsp;&nbsp;var numZeros = totalLen - numStr.length // Calculate no. of zeros<br>&nbsp;&nbsp;&nbsp;if (numZeros &gt; 0) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (var i = 1; i &lt;= numZeros; i++) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;numStr = "0" + numStr<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;return numStr<br>}</zcodeline></FONT><P><A NAME="1220892"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The following statements call the <FONT FACE="courier, couriernew, monospaced size">padZeros</FONT> function.</zparagraph></FONT><P><A NAME="1220942"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline>result=padZeros(42,4) // returns "0042"<br>result=padZeros(42,2) // returns "42"<br>result=padZeros(5,4)&nbsp; // returns "0005"</zcodeline></FONT><P><A NAME="1237543"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph><B>Example 2.</B> You can determine whether a function exists by comparing the function name to null. In the following example, <FONT FACE="courier, couriernew, monospaced size">func1</FONT> is called if the function <FONT FACE="courier, couriernew, monospaced size">noFunc</FONT> does not exist; otherwise <FONT FACE="courier, couriernew, monospaced size">func2</FONT> is called. Notice that the window name is needed when referring to the function name <FONT FACE="courier, couriernew, monospaced size">noFunc</FONT>.</zparagraph></FONT><P><A NAME="1237544"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline>if (window.noFunc == null)<br>&nbsp;&nbsp;&nbsp;func1()<br>else func2()</zcodeline></FONT><P><A NAME="1237551"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph><B>Example 3.</B> The following example creates <A HREF="../../../Client/Print/handlers.html#17093"  ><FONT FACE="courier, couriernew, monospaced size">onFocus</FONT></A> and <A HREF="../../../Client/Print/handlers.html#29096"  ><FONT FACE="courier, couriernew, monospaced size">onBlur</FONT></A> event handlers for a frame. This code exists in the same file that contains the <FONT FACE="courier, couriernew, monospaced size">FRAMESET</FONT> tag. Note that this is the only way to create <A HREF="../../../Client/Print/handlers.html#17093"  ><FONT FACE="courier, couriernew, monospaced size">onFocus</FONT></A> and <A HREF="../../../Client/Print/handlers.html#29096"  ><FONT FACE="courier, couriernew, monospaced size">onBlur</FONT></A> event handlers for a frame, because you cannot specify the event handlers in the <FONT FACE="courier, couriernew, monospaced size">FRAME</FONT> tag.</zparagraph></FONT><P><A NAME="1237558"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline>frames[0].onfocus = new Function("document.bgColor='antiquewhite'")<br>frames[0].onblur = new Function("document.bgColor='lightgrey'")</zcodeline></FONT><P><BR><P><A NAME="1194017"> </A><A NAME="apply"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="4"><B><zrefhead2>apply</zrefhead2></B></FONT><P><A NAME="1237563"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>This feature is not in the ECMA specification that corresponds to JavaScript 1.3, but is expected in the next version.</zparagraph></FONT><P><A NAME="1194019"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Allows you to apply a method of another object in the context of a different object (the calling 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="1196931"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext><I>Method of</I></ztabletext></FONT>&nbsp;</TD>    <TD VALIGN=TOP><P><A NAME="1196936"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext><A HREF="function.html#1193137"  >Function</A></ztabletext></FONT>&nbsp;</TD>  </TR>  <TR>    <TD VALIGN=TOP><P><A NAME="1196938"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext><I>Implemented in</I></ztabletext></FONT>&nbsp;</TD>    <TD VALIGN=TOP><P><A NAME="1196940"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext>JavaScript 1.3</ztabletext></FONT>&nbsp;</TD>  </TR></TABLE><P></zparagraph></FONT><P><BR><A NAME="1194032"> </A><A NAME="Syntax"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><B><zrefheader>Syntax</zrefheader></B></FONT><BR><A NAME="1194033"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline>apply(<I>thisArg</I>[, <I>argArray</I>])</zcodeline></FONT><P><BR><A NAME="1194034"> </A><A NAME="Parameters"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><B><zrefheader>Parameters</zrefheader></B></FONT><BR><A NAME="1194044"> </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><A NAME="1194037"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline>thisArg</zcodeline></FONT><P>&nbsp;</TD>    <TD VALIGN=TOP><P><A NAME="1194039"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext>Parameter for the calling object</ztabletext></FONT>&nbsp;</TD>  </TR>  <TR>    <TD VALIGN=TOP><A NAME="1194041"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline>argArray</zcodeline></FONT><P>&nbsp;</TD>    <TD VALIGN=TOP><P><A NAME="1194043"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext>An argument array for the object</ztabletext></FONT>&nbsp;</TD>  </TR></TABLE><P></zparagraph></FONT><P><BR><A NAME="1194045"> </A><A NAME="Description"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><B><zrefheader>Description</zrefheader></B></FONT><BR><A NAME="1194046"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You can assign a different <FONT FACE="courier, couriernew, monospaced size">this</FONT> object when calling an existing function. <FONT FACE="courier, couriernew, monospaced size">this</FONT> refers to the current object, the calling object. With <FONT FACE="courier, couriernew, monospaced size">apply</FONT>, you can write a method once and then inherit it in another object, without having to rewrite the method for the new object.</zparagraph></FONT><P><A NAME="1194047"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph><FONT FACE="courier, couriernew, monospaced size">apply</FONT> is very similar to <FONT FACE="courier, couriernew, monospaced size">call</FONT>, except for the type of arguments it supports. You can use an arguments array instead of a named set of parameters. With <FONT FACE="courier, couriernew, monospaced size">apply</FONT>, you can use an array literal, for example, <FONT FACE="courier, couriernew, monospaced size">apply(this, [name, value])</FONT>, or an <FONT FACE="courier, couriernew, monospaced size">Array</FONT> object, for example, <FONT FACE="courier, couriernew, monospaced size">apply(this, new Array(name, value))</FONT>.</zparagraph></FONT><P><A NAME="1209502"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You can also use <A HREF="function.html#1193312"  >arguments</A> for the <FONT FACE="courier, couriernew, monospaced size">argArray</FONT> parameter. <FONT FACE="courier, couriernew, monospaced size">arguments</FONT> is a local variable of a function. It can be used for all unspecified arguments of the called object. Thus, you do not have to know the arguments of the called object when you use the <FONT FACE="courier, couriernew, monospaced size">apply</FONT> method. You can use <FONT FACE="courier, couriernew, monospaced size">arguments</FONT> to pass all the arguments to the called object. The called object is then responsible for handling the arguments.</zparagraph></FONT><P><BR><A NAME="1194049"> </A><A NAME="Examples"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><B><zrefheader>Examples</zrefheader></B></FONT><BR><A NAME="1194050"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You can use <FONT FACE="courier, couriernew, monospaced size">apply</FONT> to chain constructors for an object, similar to Java. In the following example, the constructor for the <FONT FACE="courier, couriernew, monospaced size">product</FONT> object is defined with two parameters, <FONT FACE="courier, couriernew, monospaced size">name</FONT> and <FONT FACE="courier, couriernew, monospaced size">value</FONT>. Another object, <FONT FACE="courier, couriernew, monospaced size">prod_dept</FONT>, initializes its unique variable (<FONT FACE="courier, couriernew, monospaced size">dept</FONT>) and calls the constructor for <FONT FACE="courier, couriernew, monospaced size">product</FONT> in its constructor to initialize the other variables. In this example, the parameter <FONT FACE="courier, couriernew, monospaced size">arguments</FONT> is used for all arguments of the <FONT FACE="courier, couriernew, monospaced size">product</FONT> object's constructor.</zparagraph></FONT><P><A NAME="1194051"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline>function product(name, value){<br>&nbsp;&nbsp;&nbsp;this.name = name;<br>&nbsp;&nbsp;&nbsp;if(value &gt; 1000)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.value = 999;<br>&nbsp;&nbsp;&nbsp;else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.value = value;<br>}</zcodeline></FONT><P><A NAME="1194052"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline>function prod_dept(name, value, dept){<br>&nbsp;&nbsp;&nbsp;this.dept = dept;<br>&nbsp;&nbsp;&nbsp;product.apply(product, arguments);<br>}</zcodeline></FONT><P><A NAME="1194053"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline>prod_dept.prototype = new product();</zcodeline></FONT><P><A NAME="1194054"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline>// since 5 is less than 100 value is set<br>cheese = new prod_dept("feta", 5, "food");</zcodeline></FONT>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -