首页 > 代码库 > 函数的定义
函数的定义
<!DOCTYPE html> <html> <head> <title></title> <script type="text/javascript"> function sum(a,b){ var rescroe= a+b; return rescroe; } var c=sum(1,2); console.log(c); function args() { return arguments; } //函数内建arguments数组 console.log(args()); console.log( args(1,2,3,‘adc‘,true)); function sumonescreat() { var i, res=0; var number_of_arguments=arguments.length; for(i= 0 ; i<number_of_arguments; i++){ res+=arguments[i]; }; return res; } console.log(sumonescreat(1,2,3,4,5)); var parstr=parseInt(‘124‘); //parseInt转为整数 console.log(parstr); var goa=123; //全局变量 function f(){ //f函数 console.log(goa); //undefined var goa=23; console.log(goa); //23 } f(); var desun=function(a,b){ return a+b }; var add=desun; delete desun; console.log(typeof desun); console.log(add(1,2)) function intel_add_and(a,b) { return a()+b(); } function anum(){ return 12; } function bnum() { return 13; } console.log(intel_add_and(anum,bnum)); function callfun(a,b,c,callback) { var i,rec=[]; for(i=0; i<3; i++){ rec[i]=callback(arguments[i]*2); } return rec; } function addfun(d){ return d+1; } var mycall=callfun(1,2,3,addfun); console.log(mycall); //[357] var a=function(){ // alert(‘fdsaf‘); //自调函数 function somesetup( ) { var setup=‘doen‘; } function autowork( ) { console.log(‘worker‘); } somesetup(); return autowork(); }(); //作用域 var zuo=1; function f(){var zuoa=12; return zuo;} // console.log(f(),zuoa); 1, zuoa is not defined; console.log(f()); var zuo_a=123; function zuo_fun() { var zuo_b=124; function n() { var zuo_c=125; } } function fung() { var fung_b=‘b‘; return function(){ return fung_b; } } var funh=fung(); console.log(funh()); var n; //定义一个全局变量n; function fun_n() { //在fun_n函数里调用n(); var fun_a=‘a‘; n=function() { return fun_a; } } fun_n(); n(); console.log(n()); var funN; function fun_argu(argus) { var funN=function () { return argus; }; argus++; return funN; } var myargus=fun_argu(145); console.log(myargus()); function fun_arr() { var a=[]; var i; for (var i = 0; i < 3; i++) { a[i]=function(){ return i; } } return a; } var a=fun_arr(); console.log(a[0](),a[1](),a[2]()); //3,3,3; function fun_arr2() { var c=[]; var i; for (var i =0; i < 3; i++) { c[i]=(function(x){ return function(){ return x; } })(i); } return c; } var c=fun_arr2(); console.log(c[0](),c[1](),c[2]()); //0 1 2 function fun_arr3() { function makeClosure(x) { return function() { return x; } } var d=[]; var i; for (var i =0; i < 3; i++) { d[i]=makeClosure(i) } return d; } var d=fun_arr3(); console.log(d[0](),d[1](),d[2]()); // 0 1 2; //setValue,getValue; var setValue,getValue; (function(){ var set_value=http://www.mamicode.com/0;"rgb("+rgb.join(",")+")"; } getRGB(‘#00ff00‘); //"rgb(0,255,0)" var xa=1; function fxa() { var xa=2; function xn() { alert(xa); // body... } xn(); } fxa(); //返回2 </script> </head> <body> </body> </html>
函数的定义
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。