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

📄 function.html

📁 javascript 1.5规范
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<HTML><HEAD>        <META NAME="keywords" CONTENT="electronic commerce, ecommerce, ebusiness, e-business, e-commerce, enterprise software, net economy, Netscape, Sun Microsystems, Sun-Netscape Alliance, iPlanet, internet software">        <META NAME="description" CONTENT="The Sun-Netscape Alliance, a strategic alliance formed by America Online and Sun Microsystems, Inc., is delivering iPlanet e-commerce software and enterprise solutions that enable companies to compete successfully in the Net Economy">	<META HTTP-EQUIV="Content-Type" content="text/html; charset=ISO-8859-1">	<META NAME="TEMPLATEBASE" CONTENT="Authored in FrameMaker. Converted to HTML in WebWorks Publisher 2000. manual wdt 1.5">	<META NAME="LASTUPDATED" CONTENT="09/28/00 13:15:28">	<TITLE> Core JavaScript Reference 1.5:  </TITLE></HEAD><BODY TEXT="#000000" LINK="#666699" VLINK="#666699" ALINK="#333366" BGCOLOR="#FFFFFF"><MAINCONTENT><NAVIGATIONCONTENT>   <TABLE WIDTH="100%" BORDER="0" CELLSPACING="4">      <TR>         <TD VALIGN="TOP">            <b><a href="date.html"><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#993300">Previous</a></FONT>&nbsp;&nbsp;&nbsp;&nbsp;</b><a href="contents.html"><b><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#993300">Contents</a></FONT></b>&nbsp;&nbsp;&nbsp;&nbsp;<a href="ix.html"><b><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#993300">Index</a></FONT></b>&nbsp;&nbsp;&nbsp;&nbsp;<a href="java.html"><b><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#993300">Next</a></FONT></b>&nbsp;&nbsp;&nbsp;&nbsp;         </TD>     </TR>      <TR>         <TD VALIGN="TOP" ALIGN="RIGHT" BGCOLOR="#CCCCCC">            <FONT FACE="Arial, Helvetica, sans-serif" COLOR="#000000" SIZE="2"><B>             Core JavaScript Reference 1.5</B></FONT>         </TD>      </TR>      <TR>   </TABLE>   <BR></NAVIGATIONCONTENT><BOOKCONTENT>   <blockquote><BR><BR>      <BR><BR><P><A NAME="1193137"> </A><A NAME="Function"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="5"><B><zrefhead1>Function</zrefhead1></B></FONT><P><A NAME="1237518"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>xxx I believe that I have removed all client-specific examples from this file.</zparagraph></FONT><P><A NAME="1193161"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>Specifies a string of JavaScript code to be compiled as a function.<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 COLSPAN=2 ROWSPAN=1><P><A NAME="1193141"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext><I>Core object</I></ztabletext></FONT></TD>  </TR>  <TR>    <TD VALIGN=TOP><P><A NAME="1193145"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext><I>Implemented in</I></ztabletext></FONT>&nbsp;</TD>    <TD VALIGN=TOP><P><A NAME="1193147"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext>JavaScript 1.1, NES 2.0</ztabletext></FONT><P><A NAME="1194816"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext></ztabletext></FONT><P><A NAME="1194817"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext>JavaScript 1.2: added <A HREF="function.html#1231596"  >arity</A>, <A HREF="function.html#1196419"  >arguments.callee</A> properties; added ability to nest functions.</ztabletext></FONT><P><A NAME="1193160"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext></ztabletext></FONT><P><A NAME="1194822"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext>JavaScript 1.3: added <A HREF="function.html#1194017"  >apply</A>, <A HREF="function.html#1194243"  >call</A>, and <A HREF="function.html#1193583"  >toSource</A> methods; deprecated <A HREF="function.html#1196447"  >arguments.caller</A> property.</ztabletext></FONT><P><A NAME="1215008"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext></ztabletext></FONT><P><A NAME="1215053"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext>JavaScript 1.4: deprecated <A HREF="function.html#1193312"  >arguments</A>, <A HREF="function.html#1196419"  >arguments.callee</A>, <A HREF="function.html#1196484"  >arguments.length</A>, and <A HREF="function.html#1231596"  >arity</A> properties (<FONT FACE="courier, couriernew, monospaced size">arguments</FONT> remains a variable local to a function rather than a property of <FONT FACE="courier, couriernew, monospaced size">Function</FONT>).</ztabletext></FONT>&nbsp;</TD>  </TR>  <TR>    <TD VALIGN=TOP><P><A NAME="1196892"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext><I>ECMA version</I></ztabletext></FONT>&nbsp;</TD>    <TD VALIGN=TOP><P><A NAME="1196894"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext>ECMA-262</ztabletext></FONT>&nbsp;</TD>  </TR></TABLE><P></zparagraph></FONT><P><BR><A NAME="1213952"> </A><A NAME="Created by"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><B><zrefheader>Created by</zrefheader></B></FONT><BR><A NAME="1213953"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The <FONT FACE="courier, couriernew, monospaced size">Function</FONT> constructor:</zparagraph></FONT><P><A NAME="1213954"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline>new Function ([<I>arg1</I>[, <I>arg2</I>[, ... <I>argN</I>]],] <I>functionBody</I>)</zcodeline></FONT><P><A NAME="1214901"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The <FONT FACE="courier, couriernew, monospaced size">function</FONT> statement (see <A HREF="stmt.html#1004825"  >function</A> for details):</zparagraph></FONT><P><A NAME="1214924"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline>function <I>name</I>([<I>param</I>[, <I>param</I>[, ... <I>param</I>]]]) {<br>&nbsp;&nbsp;&nbsp;<I>statements<br></I>}</zcodeline></FONT><P><BR><A NAME="1213955"> </A><A NAME="Parameters"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><B><zrefheader>Parameters</zrefheader></B></FONT><BR><A NAME="1193175"> </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="1193168"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline>arg1, arg2, ... arg<I>N</I></zcodeline></FONT><P>&nbsp;</TD>    <TD VALIGN=TOP><P><A NAME="1193170"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext>Names to be used by the function as formal argument names. Each must be a string that corresponds to a valid JavaScript identifier; for example <FONT FACE="courier, couriernew, monospaced size">"x"</FONT> or <FONT FACE="courier, couriernew, monospaced size">"theValue"</FONT>.</ztabletext></FONT>&nbsp;</TD>  </TR>  <TR>    <TD VALIGN=TOP><A NAME="1193172"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline>functionBody</zcodeline></FONT><P>&nbsp;</TD>    <TD VALIGN=TOP><P><A NAME="1193174"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext>A string containing the JavaScript statements comprising the function definition.</ztabletext></FONT>&nbsp;</TD>  </TR>  <TR>    <TD VALIGN=TOP><A NAME="1224441"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline>name</zcodeline></FONT><P>&nbsp;</TD>    <TD VALIGN=TOP><P><A NAME="1224443"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext>The function name.</ztabletext></FONT>&nbsp;</TD>  </TR>  <TR>    <TD VALIGN=TOP><A NAME="1224437"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline>param</zcodeline></FONT><P>&nbsp;</TD>    <TD VALIGN=TOP><P><A NAME="1224439"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext>The name of an argument to be passed to the function. A function can have up to 255 arguments.</ztabletext></FONT>&nbsp;</TD>  </TR>  <TR>    <TD VALIGN=TOP><A NAME="1231929"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline>statements</zcodeline></FONT><P>&nbsp;</TD>    <TD VALIGN=TOP><P><A NAME="1231931"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext>The statements comprising the body of the function.</ztabletext></FONT>&nbsp;</TD>  </TR></TABLE><P></zparagraph></FONT><P><BR><A NAME="1193176"> </A><A NAME="Description"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><B><zrefheader>Description</zrefheader></B></FONT><BR><A NAME="1218580"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph><FONT FACE="courier, couriernew, monospaced size">Function</FONT> objects created with the <FONT FACE="courier, couriernew, monospaced size">Function</FONT> constructor are evaluated each time they are used. This is less efficient than declaring a function and calling it within your code, because declared functions are compiled.</zparagraph></FONT><P><A NAME="1218451"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>To return a value, the function must have a <A HREF="stmt.html#1004871"  >return</A> statement that specifies the value to return.</zparagraph></FONT><P><A NAME="1218452"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>All parameters are passed to functions <I>by value</I>; the value is passed to the function, but if the function changes the value of the parameter, this change is not reflected globally or in the calling function. However, if you pass an object as a parameter to a function and the function changes the object's properties, that change is visible outside the function, as shown in the following example:</zparagraph></FONT><P><A NAME="1228173"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline>function myFunc(theObject) {<br>&nbsp;&nbsp;&nbsp;theObject.make="Toyota"<br>}<br><br>mycar = {make:"Honda", model:"Accord", year:1998}<br>x=mycar.make &nbsp;&nbsp;&nbsp;&nbsp;// returns Honda<br>myFunc(mycar) &nbsp;&nbsp;&nbsp;// pass object mycar to the function<br>y=mycar.make &nbsp;&nbsp;&nbsp;&nbsp;// returns Toyota (prop was changed by the function)</zcodeline></FONT><P><A NAME="1224640"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The <FONT FACE="courier, couriernew, monospaced size">this</FONT> keyword does not refer to the currently executing function, so you must refer to <FONT FACE="courier, couriernew, monospaced size">Function</FONT> objects by name, even within the function body.</zparagraph></FONT><P><P><A NAME="1231645"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><B><zheadrunin>Accessing a function's arguments with the arguments array. </zheadrunin></B></FONT><A NAME="1231646"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You can refer to a function's arguments within the function by using the <FONT FACE="courier, couriernew, monospaced size">arguments</FONT> array. See <A HREF="function.html#1193312"  >arguments</A>.</zparagraph></FONT><P><P><A NAME="1205610"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><B><zheadrunin>Specifying arguments with the Function constructor. </zheadrunin></B></FONT><A NAME="1205611"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The following code creates a <FONT FACE="courier, couriernew, monospaced size">Function</FONT> object that takes two arguments.</zparagraph></FONT><P><A NAME="1205612"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline>var multiply = new Function("x", "y", "return x * y")</zcodeline></FONT><P><A NAME="1207137"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The arguments <FONT FACE="courier, couriernew, monospaced size">"x"</FONT> and <FONT FACE="courier, couriernew, monospaced size">"y"</FONT> are formal argument names that are used in the function body, <FONT FACE="courier, couriernew, monospaced size">"return x * y"</FONT>.</zparagraph></FONT><P><A NAME="1207331"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The preceding code assigns a function to the variable <FONT FACE="courier, couriernew, monospaced size">multiply</FONT>. To call the <FONT FACE="courier, couriernew, monospaced size">Function</FONT> object, you can specify the variable name as if it were a function, as shown in the following examples.</zparagraph>

⌨️ 快捷键说明

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