2.17.JavaScript--JavaScript.函数arguments对象
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8"> <title>JavaScript.函数arguments对象</title> </head> <body> <script type="text/javascript"> /* function sayHello(){ //alert(arguments.length); alert(arguments[0] + "-" + arguments[1] + "-" + arguments[2]); }; sayHello("xiaozhanga4","apple","google"); */ /* function func(){ for(var i=0,arrs=[];i<arguments.length;i++){ arrs.push(arguments[i]); } alert(arrs.join(" - ")); } func("facebook","baidu","renren"); */ //模拟函数重载 function funcTest(){ switch(arguments.length){ case 0: return alert("没有参数"); case 1: return alert("1个参数:" + arguments[0]); case 2: return alert("2个参数:" + arguments[0] + "-" + arguments[1]); case 3: return alert("3个参数:" + arguments[0] + "-" + arguments[1] + "-" + arguments[2]); } } funcTest(); funcTest(1); funcTest(1,2); funcTest(1,2,3); </script> </body> </html>
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8"> <title>arguments对象</title> </head> <body> <ul> <li id="apple">apple</li> <li id="google">google</li> <li id="facebook">facebook</li> </ul> <script type="text/javascript"> window['jquery'] = {}; window['jquery']['$'] = $; function $(){ var elements = []; for(var i=0;i<arguments.length;i++){//循环得到整个参数个数 element = arguments[i];//把数组赋给element if(typeof element == "string"){//判断数组是否为字符串类型 element = document.getElementById(element); } if(arguments.length == 1){ return element; } elements.push(element); } return elements; } var items = jquery.$("apple","google","facebook"); for(var i=0;i<items.length;i++){ items[i].style.color = "red"; } jquery.$(""); </script> </body> </html>