日期:2014-05-16 浏览次数:20530 次
//arguments 对象的用法。
function ArgTest(a, b){
var i, s = "The ArgTest function expected ";
var numargs = arguments.length; // 获取被传递参数的数值。
var expargs = ArgTest.length; // 获取期望参数的数值。
if (expargs < 2)
s += expargs + " argument. ";
else
s += expargs + " arguments. ";
if (numargs < 2)
s += numargs + " was passed.";
else
s += numargs + " were passed.";
s += "\n\n";
for (i =0 ; i < numargs; i++){ // 获取参数内容。
s += " Arg " + i + " = " + arguments[i] + "\n";
}
return(s); // 返回参数列表。
}
alert(ArgTest('a1', 'b1'));
alert(ArgTest('a2', 'b2', 'c2'));
alert(ArgTest('a3'));
Array.prototype.selfvalue = 1;
alert('new Array().selfvalue=' + new Array().selfvalue);
function testAguments(){
alert(arguments.selfvalue);
}
testAguments('a4');
alert(arguments instanceof Array); alert(arguments instanceof Object);
function callerDemo() {
if (callerDemo.caller) {
var a= callerDemo.caller.toString();
alert(a);
} else {
alert("this is a top function");
}
}
function handleCaller() {
callerDemo();
}
handleCaller();
//callee可以打印其本身
function calleeDemo() {
alert(arguments.callee);
}
//用于验证参数
function calleeLengthDemo(arg1, arg2) {
if (arguments.length==arguments.callee.length) {
alert("验证形参和实参长度正确!");
} else {
alert("实参长度:" +arguments.length +"形参长度: " +arguments.callee.length);
}
}
//递归计算
var sum = function(n){
if (n <= 1)
return 1;
else
return n + arguments.callee(n - 1);
}
calleeDemo();
calleeLengthDemo('a5', 'b5');
calleeLengthDemo('a6');
alert(sum(100));