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

📄 function.html

📁 javascript 1.5规范
💻 HTML
📖 第 1 页 / 共 5 页
字号:
</FONT><P><A NAME="1207574"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline>var theAnswer = multiply(7,6)</zcodeline></FONT><P><A NAME="1207332"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline>var myAge = 50<br>if (myAge &gt;=39) {myAge=multiply (myAge,.5)}</zcodeline></FONT><P><P><A NAME="1224456"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><B><zheadrunin>Assigning a function to a variable with the Function constructor. </zheadrunin></B></FONT><A NAME="1224457"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Suppose you create the variable <FONT FACE="courier, couriernew, monospaced size">multiply</FONT> using the <FONT FACE="courier, couriernew, monospaced size">Function</FONT> constructor, as shown in the preceding section:</zparagraph></FONT><P><A NAME="1217471"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline>var multiply = new Function("x", "y", "return x * y")</zcodeline></FONT><P><A NAME="1217469"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>This is similar to declaring the following function:</zparagraph></FONT><P><A NAME="1206153"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline>function multiply(x,y) {<br>&nbsp;&nbsp;&nbsp;return x*y<br>}</zcodeline></FONT><P><A NAME="1193268"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Assigning a function to a variable using the <FONT FACE="courier, couriernew, monospaced size">Function</FONT> constructor is similar to declaring a function with the <FONT FACE="courier, couriernew, monospaced size">function</FONT> statement, but they have differences:</zparagraph></FONT><P><ul><A NAME="1193269"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><B>  <li></B><zsmartlist1>When you assign a function to a variable using <FONT FACE="courier, couriernew, monospaced size">var multiply = new Function("...")</FONT>, <FONT FACE="courier, couriernew, monospaced size">multiply</FONT> is a variable for which the current value is a reference to the function created with <FONT FACE="courier, couriernew, monospaced size">new Function()</FONT>.</zsmartlist1></FONT><P><A NAME="1193270"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><B>  <li></B><zsmartlist1>When you create a function using <FONT FACE="courier, couriernew, monospaced size">function multiply() {...}</FONT>, <FONT FACE="courier, couriernew, monospaced size">multiply</FONT> is not a variable, it is the name of a function.</zsmartlist1></FONT><P></ul><P><A NAME="1225385"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><B><zheadrunin>Nesting functions. </zheadrunin></B></FONT><A NAME="1225386"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You can nest a function within a function. The nested (inner) function is private to its containing (outer) function:</zparagraph></FONT><P><ul><A NAME="1226496"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><B>  <li></B><zsmartlist1>The inner function can be accessed only from statements in the outer function.</zsmartlist1></FONT><P><A NAME="1225764"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><B>  <li></B><zsmartlist1>The inner function can use the arguments and variables of the outer function. The outer function cannot use the arguments and variables of the inner function.</zsmartlist1></FONT><P></ul><A NAME="1226522"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The following example shows nested functions:</zparagraph></FONT><P><A NAME="1227746"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline>function addSquares (a,b) {<br>&nbsp;&nbsp;&nbsp;function square(x) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return x*x<br>&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;return square(a) + square(b)<br>}<br>a=addSquares(2,3) // returns 13<br>b=addSquares(3,4) // returns 25<br>c=addSquares(4,5) // returns 41</zcodeline></FONT><P><A NAME="1226900"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>When a function contains a nested function, you can call the outer function and specify arguments for both the outer and inner function:</zparagraph></FONT><P><A NAME="1226901"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline>function outside(x) {<br>&nbsp;&nbsp;&nbsp;function inside(y) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return x+y<br>&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;return inside<br>}<br>result=outside(3)(5) // returns 8</zcodeline></FONT><P><P><A NAME="1237524"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><B><zheadrunin>Specifying an event handler with a Function object. </zheadrunin></B></FONT><A NAME="1237525"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The following code assigns a function to a window's <A HREF="../../../Client/Print/handlers.html#17093"  ><FONT FACE="courier, couriernew, monospaced size">onFocus</FONT></A> event handler (the event handler must be spelled in all lowercase):</zparagraph></FONT><P><A NAME="1237529"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline>window.onfocus = new Function("document.bgColor='antiquewhite'")</zcodeline></FONT><P><A NAME="1237530"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>If a function is assigned to a variable, you can assign the variable to an event handler. The following code assigns a function to the variable <FONT FACE="courier, couriernew, monospaced size">setBGColor</FONT>.</zparagraph></FONT><P><A NAME="1237531"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline>var setBGColor = new Function("document.bgColor='antiquewhite'")</zcodeline></FONT><P><A NAME="1237532"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You can use this variable to assign a function to an event handler in either of the following ways:</zparagraph></FONT><P><A NAME="1237533"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline>document.form1.colorButton.onclick=setBGColor</zcodeline></FONT><P><A NAME="1237534"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline>&lt;INPUT NAME="colorButton" TYPE="button"<br>&nbsp;&nbsp;&nbsp;VALUE="Change background color"<br>&nbsp;&nbsp;&nbsp;onClick="setBGColor()"&gt;</zcodeline></FONT><P><A NAME="1237535"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Once you have a reference to a <FONT FACE="courier, couriernew, monospaced size">Function</FONT> object, you can use it like a function and it will convert from an object to a function:</zparagraph></FONT><P><A NAME="1237536"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline>window.onfocus()</zcodeline></FONT><P><A NAME="1237537"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Event handlers do not take arguments, so you cannot declare any arguments in a <FONT FACE="courier, couriernew, monospaced size">Function</FONT> constructor for an event handler. For example, you cannot call the function <FONT FACE="courier, couriernew, monospaced size">multiply</FONT> by setting a button's <FONT FACE="courier, couriernew, monospaced size">onclick</FONT> property as follows:</zparagraph></FONT><P><A NAME="1237538"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline>document.form1.button1.onclick=multFun(5,10)</zcodeline></FONT><P><BR><A NAME="1216173"> </A><A NAME="Backward Compatibility"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><B><zrefheader>Backward Compatibility</zrefheader></B></FONT><BR><P><A NAME="1225182"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><B><zheadrunin>JavaScript 1.1 and earlier versions. </zheadrunin></B></FONT><A NAME="1225160"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You cannot nest a function statement in another statement or in itself.</zparagraph></FONT><P><BR><A NAME="1203656"> </A><A NAME="Property Summary"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><B><zrefheader>Property Summary</zrefheader></B></FONT><BR><A NAME="1203704"> </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="1203659"> </A><P ALIGN="LEFT"><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><B><ztablehead>Property</ztablehead></B></FONT></TH>    <TH VALIGN=TOP ALIGN=LEFT><A NAME="1203661"> </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="1203666"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline><A HREF="function.html#1193312"  >arguments</A></zcodeline></FONT><P>&nbsp;</TD>    <TD VALIGN=TOP><P><A NAME="1203668"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext>An array corresponding to the arguments passed to a function.</ztabletext></FONT>&nbsp;</TD>  </TR>  <TR>    <TD VALIGN=TOP><A NAME="1203673"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline><A HREF="function.html#1196419"  >arguments.callee</A></zcodeline></FONT><P>&nbsp;</TD>    <TD VALIGN=TOP><P><A NAME="1203675"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext>Specifies the function body of the currently executing function.</ztabletext></FONT>&nbsp;</TD>  </TR>  <TR>    <TD VALIGN=TOP><A NAME="1203680"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline><A HREF="function.html#1196447"  >arguments.caller</A></zcodeline></FONT><P>&nbsp;</TD>    <TD VALIGN=TOP><P><A NAME="1203682"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext>Specifies the name of the function that invoked the currently executing function.</ztabletext></FONT>&nbsp;</TD>  </TR>  <TR>    <TD VALIGN=TOP><A NAME="1203687"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline><A HREF="function.html#1196484"  >arguments.length</A></zcodeline></FONT><P>&nbsp;</TD>    <TD VALIGN=TOP><P><A NAME="1203689"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext>Specifies the number of arguments passed to the function.</ztabletext></FONT>&nbsp;</TD>  </TR>  <TR>    <TD VALIGN=TOP><A NAME="1203694"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline><A HREF="function.html#1231596"  >arity</A></zcodeline></FONT><P>&nbsp;</TD>    <TD VALIGN=TOP><P><A NAME="1203696"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext>Specifies the number of arguments expected by the function.</ztabletext></FONT>&nbsp;</TD>  </TR>  <TR>    <TD VALIGN=TOP><A NAME="1204673"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline><A HREF="function.html#1204601"  >constructor</A></zcodeline></FONT><P>&nbsp;</TD>    <TD VALIGN=TOP><P><A NAME="1204675"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext>Specifies the function that creates an object's prototype.</ztabletext></FONT>&nbsp;</TD>  </TR>  <TR>    <TD VALIGN=TOP><A NAME="1217139"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline><A HREF="function.html#1216661"  >length</A></zcodeline></FONT><P>&nbsp;</TD>    <TD VALIGN=TOP><P><A NAME="1217141"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext>Specifies the number of arguments expected by the function.</ztabletext></FONT>&nbsp;</TD>  </TR>  <TR>    <TD VALIGN=TOP><A NAME="1203701"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline><A HREF="function.html#1193426"  >prototype</A></zcodeline></FONT><P>&nbsp;</TD>    <TD VALIGN=TOP><P><A NAME="1203703"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext>Allows the addition of properties to a <FONT FACE="courier, couriernew, monospaced size">Function</FONT> object.</ztabletext></FONT>&nbsp;</TD>  </TR></TABLE><P></zparagraph></FONT><P><BR><A NAME="1203709"> </A><A NAME="Method Summary"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><B><zrefheader>Method Summary</zrefheader></B></FONT><BR>

⌨️ 快捷键说明

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