码迷,mamicode.com
首页 > Web开发 > 详细

js中的arguments对象

时间:2017-07-22 12:14:06      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:js函数   hub   set   cti   html   har   blog   doctype   bin   

在Javascript中没有函数重载,而arguments对象弥补了这点不足。

js函数的参数是一个数组,在参数个数不固定的情况下,只需要给方法传递不同元素个数的数组即可。即使声明函数时没有形式参数,在调用时也可以传递参数,这些参数存放在arguments对象中。通过数组的下标可以访问传入方法的参数,而参数的个数可以通过arguments.length来获取。

 

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Document</title>
 6 <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
 7 <script type="text/javascript">
 8     $(function(){
 9          $(#btnTest).bind(click,function(){
10              sayhello(hello,world);
11          }) 
12     });
13     function sayhello(){
14         console.log(arguments[0] + , + arguments[1]);
15     }
16 </script>
17 </head>
18 <body>
19     <input id=‘btnTest‘ type=‘button‘ value=‘click me!‘ />
20 </body>
21 </html>

 查看运行效果

js中的arguments对象

标签:js函数   hub   set   cti   html   har   blog   doctype   bin   

原文地址:http://www.cnblogs.com/yaotome/p/7220563.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!