可以这样子调用函数么?很莫名。。。。。。在线等

yinleiyoung 2006-08-30 02:45:52
有这样一个js的function:

function handlerRequestURLXML( sendurl, responseHandler, urlparam )
{
if( sendurl.length == 0 )
{
responseHandler();
return;
}
else
{
if( urlparam == "undefined" || urlparam == null )
{
urlparam = "";
}
var req = newXMLHttpRequest();
var handler = getReadyStateHandler(req, responseHandler);
req.onreadystatechange = handler;
req.open("POST", sendurl, true);
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
req.send(urlparam);
}
}

明明是有三个参数,可是为什么很多调用它的jsp页面都只传递两个?

url = "billDeployInputDetail.do?transferId="+txtValue.trim();
handlerRequestURLXML( url, setFields );

(其中的setFields是另一个方法的名字)

我已经查过了,只有一个名叫handlerRequestURLXML的函数
怎么回事啊?可以这样么?为什么?
...全文
172 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
pli0825 2006-09-01
  • 打赏
  • 举报
回复
mark!!
终于明白了。

以前偶也觉得奇怪涅
xuzuning 2006-08-30
  • 打赏
  • 举报
回复
当然可以
在js中,调用时的参数个数可以少于或多于原型所定义的个数
当少于时(通常称为“缺省”),则需要编程检查该参数是否已传入

function handlerRequestURLXML( sendurl, responseHandler, urlparam ) {
....
if( urlparam == "undefined" || urlparam == null ) // 这里已对缺省的参数做了判断
rcom10002 2006-08-30
  • 打赏
  • 举报
回复
http://community.csdn.net/Expert/TopicView1.asp?id=4984880

这个帖子有这本书,呵呵,你去下一下看看
penglewen 2006-08-30
  • 打赏
  • 举报
回复
因为javascript 是一种弱语言类型的.你可以不传参数都行的.
rcom10002 2006-08-30
  • 打赏
  • 举报
回复
这个是《JavaScript: The Definitive Guide, 4th Edition》中关于参数对象的内容,看一下就明白了,爱传几个传几个,而且都能调用成功。

7.4 Function Arguments: The Arguments Object
Within the body of a function, the identifier arguments always has special meaning. arguments is a special property of the call object that refers to an object known as the Arguments object. The Arguments object is like an array that allows the argument values passed to the function to be retrieved by number, but it is not actually an Array object. The Arguments object also defines an additional callee property, described later.
Although a JavaScript function is defined with a fixed number of named arguments, it can be passed any number of arguments when it is invoked. The arguments[] array allows full access to these argument values, even when some are unnamed. Suppose you define a function f that expects to be passed one argument, x. If you invoke this function with two arguments, the first argument is accessible within the function by the parameter name x or as arguments[0]. The second argument is accessible only as arguments[1]. Furthermore, like all arrays, arguments has a length property that specifies the number of elements it contains. Thus, within the body of our function f, invoked with two arguments, arguments.length has the value 2.
The arguments[] array is useful in a number of ways. The following example shows how you can use it to check that a function is invoked with the correct number of arguments, since JavaScript doesn't do this for you:
function f(x, y, z)
{
// First, check that the right number of arguments were passed
if (arguments.length != 3) {
throw new Error("function f called with " + arguments.length +
"arguments, but it expects 3 arguments.");
}
// Now do the actual function...
}
The arguments[] array also opens up an important possibility for JavaScript functions: they can be written so that they work with any number of arguments. Here's an example that shows how you can write a simple max( ) function that accepts any number of arguments and returns the value of the largest argument it is passed (see also the built-in function Math.max( ), which in ECMAScript v3 also accepts any number of arguments):
function max( )
{
var m = Number.NEGATIVE_INFINITY;
// Loop through all the arguments, looking for, and
// remembering, the biggest
for(var i = 0; i < arguments.length; i++)
if (arguments[i] > m) m = arguments[i];
// Return the biggest
return m;
}
var largest = max(1, 10, 100, 2, 3, 1000, 4, 5, 10000, 6);
You can also use the arguments[] array to write functions that expect a fixed number of named arguments followed by an arbitrary number of unnamed arguments.
Throughout this section we've been referring to the "arguments array." Keep in mind, however, that arguments is not really an array; it is an Arguments object. Each Arguments object defines numbered array elements and a length property, but it is not technically an array -- it is better to think of it as an object that happens to have some numbered properties. The ECMAScript specification does not require the Arguments object to implement any of the special behavior that arrays do. Although you can assign a value to the arguments.length property, for example, ECMAScript does not require you to do so to actually alter the number of array elements defined in the object. (See Chapter 9 for an explanation of the special behavior of the length property of true Array objects.)
The Arguments object has one very unusual feature. When a function has named arguments, the array elements of the Arguments object are synonyms for the local variables that hold the function arguments. The arguments[] array and the argument named arguments are two different ways of referring to the same variable. Changing the value of an argument with an argument name changes the value that is retrieved through the arguments[] array. Changing the value of an argument through the arguments[] array changes the value that is retrieved by the argument name. For example:
function f(x) {
alert(x); // Displays the initial value of the argument
arguments[0] = null; // Changing the array element also changes x
alert(x); // Now displays "null"
}
7.4.1 The callee Property
In addition to its array elements, the Arguments object defines a callee property that refers to the function that is currently being executed. This is useful, for example, to allow unnamed functions to invoke themselves recursively. For instance, here is an unnamed function literal that computes factorials:
function(x) {
if (x <= 1) return 1;
return x * arguments.callee(x-1);
}
The callee property is defined by ECMAScript v1 and implemented in JavaScript 1.2.

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧