标签:
3.7函数
3.7.1 理解参数
arguments对象与命名参数的关系:
1 function say_hello(var1, var2, var3) { 2 var len = arguments.length; 3 alert(len); 4 for (i in arguments) { 5 alert(arguments[i]); 6 } 7 alert(var1); 8 alert(var2); 9 alert(var3); 10 } 11 12 say_hello(); //0, undefined, undefined, undefined 13 say_hello("first"); // 1, first, first, undefined, undefined 14 say_hello("first", "second", "third"); // 3, first, second, third, first, second, third 15 say_hello("first", "second", "third", "forth");// 4, first, second, third, forth, first, second, third
1 function say_hello(var1, var2, var3) { 2 for (i in arguments) { 3 arguments[i] = "change"; 4 } 5 alert(var1 + var2 + var3); 6 } 7 8 say_hello(1,2,3); //changechangechange
3.7.2 没有重载
没有重载
1 function overLoad (var1) { 2 alert("This is the first function"); 3 } 4 5 function overLoad(var1, var2) { 6 alert("This is the second function"); 7 } 8 9 overLoad(1); //This is the second function
模拟重载
1 function overLoad () { 2 if (arguments.length === 0) { 3 alert("first"); 4 } else if (arguments.length === 1) { 5 alert("second"); 6 } else { 7 alert("third"); 8 } 9 } 10 11 overLoad(); //first 12 overLoad(1); //second 13 overLoad(1,2,3,4); //third
JavaScript 高级程序设计(第3版)笔记——chapter3:基本概念(函数部分)
标签:
原文地址:http://www.cnblogs.com/HiuYanChong/p/5285270.html