寻找薛定谔的猫

导航

 

在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>

 查看运行效果

posted on 2017-07-22 10:42  teagueli  阅读(154)  评论(0编辑  收藏  举报