📄 function.html
字号:
<P><A NAME="1194055"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline>// since 5000 is above 1000, value will be 999<br>car = new prod_dept("honda", 5000, "auto");</zcodeline></FONT><P><BR><A NAME="1194056"> </A><A NAME="See also"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><B><zrefheader>See also</zrefheader></B></FONT><BR><A NAME="1194060"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph><A HREF="function.html#1194243" >Function.call</A></zparagraph></FONT><P><BR><P><A NAME="1193312"> </A><A NAME="arguments"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="4"><B><zrefhead2>arguments</zrefhead2></B></FONT><P><A NAME="1193325"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>An array corresponding to the arguments passed to 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><P><A NAME="1228902"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext><I>Local variable of</I></ztabletext></FONT> </TD> <TD VALIGN=TOP><P><A NAME="1228907"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext>All function objects</ztabletext></FONT> </TD> </TR> <TR> <TD VALIGN=TOP><P><A NAME="1193315"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext><I>Property of</I></ztabletext></FONT> </TD> <TD VALIGN=TOP><P><A NAME="1193320"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext><A HREF="function.html#1193137" >Function</A> (deprecated)</ztabletext></FONT> </TD> </TR> <TR> <TD VALIGN=TOP><P><A NAME="1193322"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext><I>Implemented in</I></ztabletext></FONT> </TD> <TD VALIGN=TOP><P><A NAME="1193324"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext>JavaScript 1.1, NES 2.0</ztabletext></FONT><P><A NAME="1194829"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext></ztabletext></FONT><P><A NAME="1194830"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext>JavaScript 1.2: added <A HREF="function.html#1196419" >arguments.callee</A> property.</ztabletext></FONT><P><A NAME="1210050"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext></ztabletext></FONT><P><A NAME="1212031"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext>JavaScript 1.3: deprecated <A HREF="function.html#1196447" >arguments.caller</A> property; removed support for argument names and local variable names as properties of the <FONT FACE="courier, couriernew, monospaced size">arguments</FONT> array.</ztabletext></FONT><P><A NAME="1212032"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext></ztabletext></FONT><P><A NAME="1210081"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext>JavaScript 1.4: deprecated <FONT FACE="courier, couriernew, monospaced size">arguments</FONT>, <A HREF="function.html#1196419" >arguments.callee</A>, and <A HREF="function.html#1196484" >arguments.length</A> as properties of <FONT FACE="courier, couriernew, monospaced size">Function</FONT>; retained <FONT FACE="courier, couriernew, monospaced size">arguments</FONT> as a local variable of a function and <A HREF="function.html#1196419" >arguments.callee</A> and <A HREF="function.html#1196484" >arguments.length</A> as properties of this variable.</ztabletext></FONT> </TD> </TR> <TR> <TD VALIGN=TOP><P><A NAME="1196948"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext><I>ECMA version</I></ztabletext></FONT> </TD> <TD VALIGN=TOP><P><A NAME="1196950"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext>ECMA-262</ztabletext></FONT> </TD> </TR></TABLE><P></zparagraph></FONT><P><BR><A NAME="1211023"> </A><A NAME="Description"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><B><zrefheader>Description</zrefheader></B></FONT><BR><A NAME="1230788"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The <FONT FACE="courier, couriernew, monospaced size">arguments</FONT> array is a local variable available within all function objects; <FONT FACE="courier, couriernew, monospaced size">arguments</FONT> as a property of <FONT FACE="courier, couriernew, monospaced size">Function</FONT> is no longer used.</zparagraph></FONT><P><A NAME="1229024"> </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. This array contains an entry for each argument passed to the function. For example, if a function is passed three arguments, you can refer to the arguments as follows:</zparagraph></FONT><P><A NAME="1229025"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline>arguments[0]<br>arguments[1]<br>arguments[2]</zcodeline></FONT><P><A NAME="1229074"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The <FONT FACE="courier, couriernew, monospaced size">arguments</FONT> array is available only within a function body. Attempting to access the <FONT FACE="courier, couriernew, monospaced size">arguments</FONT> array outside a function declaration results in an error.</zparagraph></FONT><P><A NAME="1229290"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You can use the <FONT FACE="courier, couriernew, monospaced size">arguments</FONT> array if you call a function with more arguments than it is formally declared to accept. This technique is useful for functions that can be passed a variable number of arguments. You can use <FONT FACE="courier, couriernew, monospaced size">arguments.length</FONT> to determine the number of arguments passed to the function, and then process each argument by using the <FONT FACE="courier, couriernew, monospaced size">arguments</FONT> array. (To determine the number of arguments declared when a function was defined, use the <A HREF="function.html#1216661" >Function.length</A> property.)</zparagraph></FONT><P><A NAME="1193330"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The <FONT FACE="courier, couriernew, monospaced size">arguments</FONT> array has the following properties:</zparagraph></FONT><P><P><A NAME="1211223"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><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="1211170"> </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="1211172"> </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><P><A NAME="1211177"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext><A HREF="function.html#1196419" >arguments.callee</A></ztabletext></FONT> </TD> <TD VALIGN=TOP><P><A NAME="1211179"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext>Specifies the function body of the currently executing function.</ztabletext></FONT> </TD> </TR> <TR> <TD VALIGN=TOP><P><A NAME="1211184"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext><A HREF="function.html#1196447" >arguments.caller</A></ztabletext></FONT> </TD> <TD VALIGN=TOP><P><A NAME="1211186"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext>Specifies the name of the function that invoked the currently executing function. (Deprecated)</ztabletext></FONT> </TD> </TR> <TR> <TD VALIGN=TOP><P><A NAME="1211191"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext><A HREF="function.html#1196484" >arguments.length</A></ztabletext></FONT> </TD> <TD VALIGN=TOP><P><A NAME="1211193"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><ztabletext>Specifies the number of arguments passed to the function.</ztabletext></FONT> </TD> </TR></TABLE><P></FONT><P><BR><A NAME="1211676"> </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="1228978"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><B><zheadrunin>JavaScript 1.3 and earlier versions. </zheadrunin></B></FONT><A NAME="1228979"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>In addition to being available as a local variable, the <FONT FACE="courier, couriernew, monospaced size">arguments</FONT> array is also a property of the <FONT FACE="courier, couriernew, monospaced size">Function</FONT> object and can be preceded by the function name. For example, if a function <FONT FACE="courier, couriernew, monospaced size">myFunc</FONT> is passed three arguments named <FONT FACE="courier, couriernew, monospaced size">arg1</FONT>, <FONT FACE="courier, couriernew, monospaced size">arg2</FONT>, and <FONT FACE="courier, couriernew, monospaced size">arg3</FONT>, you can refer to the arguments as follows:</zparagraph></FONT><P><A NAME="1229046"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline>myFunc.arguments[0]<br>myFunc.arguments[1]<br>myFunc.arguments[2]</zcodeline></FONT><P><P><A NAME="1211677"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><B><zheadrunin>JavaScript 1.1 and 1.2. </zheadrunin></B></FONT><A NAME="1212477"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>The following features, which were available in JavaScript 1.1 and JavaScript 1.2, have been removed:</zparagraph></FONT><P><ul><A NAME="1214329"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><B> <li></B><zsmartlist1>Each local variable of a function is a property of the <FONT FACE="courier, couriernew, monospaced size">arguments</FONT> array. For example, if a function <FONT FACE="courier, couriernew, monospaced size">myFunc</FONT> has a local variable named <FONT FACE="courier, couriernew, monospaced size">myLocalVar</FONT>, you can refer to the variable as <FONT FACE="courier, couriernew, monospaced size">arguments.myLocalVar</FONT>.</zsmartlist1></FONT><P><A NAME="1211678"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><B> <li></B><zsmartlist1>Each formal argument of a function is a property of the <FONT FACE="courier, couriernew, monospaced size">arguments</FONT> array. For example, if a function <FONT FACE="courier, couriernew, monospaced size">myFunc</FONT> has two arguments named <FONT FACE="courier, couriernew, monospaced size">arg1</FONT> and <FONT FACE="courier, couriernew, monospaced size">arg2</FONT>, you can refer to the arguments as <FONT FACE="courier, couriernew, monospaced size">arguments.arg1</FONT> and <FONT FACE="courier, couriernew, monospaced size">arguments.arg2</FONT>. (You can also refer to them as <FONT FACE="courier, couriernew, monospaced size">arguments[0]</FONT> and <FONT FACE="courier, couriernew, monospaced size">arguments[1]</FONT>.)</zsmartlist1></FONT><P></ul><BR><A NAME="1211592"> </A><A NAME="Examples"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><B><zrefheader>Examples</zrefheader></B></FONT><BR><A NAME="1229844"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph><B>Example 1.</B> This example defines a function that concatenates several strings. The only formal argument for the function is a string that specifies the characters that separate the items to concatenate. The function is defined as follows:</zparagraph></FONT><P><A NAME="1229845"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline>function myConcat(separator) {<br> result="" // initialize list<br> // iterate through arguments<br> for (var i=1; i<arguments.length; i++) {<br> result += arguments[i] + separator<br> }<br> return result<br>}</zcodeline></FONT><P><A NAME="1229846"> </A><FONT FACE="Arial, Helvetica, sans-serif" SIZE="2"><zparagraph>You can pass any number of arguments to this function, and it creates a list using each argument as an item in the list.</zparagraph></FONT><P><A NAME="1229847"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline>// returns "red, orange, blue, "<br>myConcat(", ","red","orange","blue")</zcodeline></FONT><P><A NAME="1229848"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline>// returns "elephant; giraffe; lion; cheetah;"<br>myConcat("; ","elephant","giraffe","lion", "cheetah")</zcodeline></FONT><P><A NAME="1229849"></A><FONT FACE="courier, couriernew, monospaced size" SIZE="2"><zcodeline>// returns "sage. basil. oregano. pepper. parsley. "<br>myConcat(". ","sage","basil","oregano", "pepper", "parsley")</zcodeline>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -