标签:arguments code tle var head doctype add function rgs
1、Js中函数的重载:JS中的函数,参数是没有要求的,一个函数假设定义了几个参数,那么调用者可以传,也可以不传,也可以只专一个两....还可以传很多个,所以我们不能依赖参数的个数来区别两个同名函数(下方代码理解)
<!DOCTYPE html>
<html>
<head>
<title>Function Example 7</title>
<script type="text/javascript">
function fa(a, b)
{
alert(a + b);
}
function fa()
{
alert(10);
}
fa(300,600);
var fb = function(a, b){alert(a + b)};
fb = function(){alert(10);};
fb(12, 16); //10
var fc = new Function(a, b, "alert(a+b)");
fc = new Function("alert(10)");
//JS中的函数,参数是没有要求的,一个函数假设定义了几个参数,那么调用者可以传,也可以不传,也可以只专一个两....还可以传很多个,所以我们不能依赖参数的个数来区别两个同名函数
//JS中的函数的重载模拟:
function doAdd() {
//arguments 对象:参数数组。
if(arguments.length == 1)
{
alert(arguments[0] + 10);
}
else if (arguments.length == 2)
{
alert(arguments[0] + arguments[1]);
}
}
//调用一个函数,通过传参的个数而得到了两种结果。
doAdd(10); //20
doAdd(30, 20); //50
</script>
</head>
<body>
</body>
</html>
2、js中函数的参数数组(arguments)的访问
<!DOCTYPE html>
<html>
<head>
<title>Function Example 5</title>
<script type="text/javascript">
//arguments参数数组
function sayHi() {
alert("Hello " + arguments[0] + ", " + arguments[1]);//参数数组的访问
}
sayHi("Nicholas", "how are you today?");
function howManyArgs() {
alert(arguments.length);//参数数组的长度
}
howManyArgs("ing", 45); //2
howManyArgs(); //0
howManyArgs(12); //1
</script>
</head>
<body>
</body>
</html>
标签:arguments code tle var head doctype add function rgs
原文地址:https://www.cnblogs.com/fllowerqq/p/8971989.html