Home > Web Front-end > JS Tutorial > The arguments object verifies whether the parameters of the function are legal_javascript skills

The arguments object verifies whether the parameters of the function are legal_javascript skills

WBOY
Release: 2016-05-16 15:52:39
Original
1334 people have browsed it

Use the arguments object to verify whether the parameters of the function are legal

<script>
function sum(arg1,arg2) //加法函数
{
var realArgCount = arguments.length; //调用函数时传递的实参个数
var frmArgCount = sum.length; //函数定义时的形参个数
if(realArgCount < frmArgCount) //如果实际参数个数小于形参个数
{
var e = new Error(); //定义错误信息,然后抛出
e.number = 1000001; //错误编号
e.message = "实际参数个数不符合要求!" //错误消息
throw e;
}
return arguments[0] + arguments[1];//参数符合要求则从arguments对象中提取实参并返回两者的和
}
try
{
document.write("<p><h1>arguments对象测试</h1></p>"); //输出标题
document.write("正确调用的结果:"+sum(10,20));//输出正确调用的结果
document.write("<br>不符合规则的调用结果:"); //人为引发一个不符合规则的调用方式
document.write(sum(10));
}
catch(e) //捕捉错误信息
{
alert(e.number+"错误号:"+e.message);
}
</script>
Copy after login

The above is the entire content of this article, I hope you all like it.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template