码迷,mamicode.com
首页 > 其他好文 > 详细

函数的定义

时间:2017-02-08 14:18:51      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:ret   regex   logs   value   argument   dsa   rgb   全局变量   intel   

<!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=0;

           getValue=function() {
              
              return set_value;
           }
           setValue=function (v) {
         	 set_value=v;
         }  

        })();

        getValue();
        console.log(getValue());     //0
        setValue(1212);
        getValue();  
        console.log(getValue());       //1212     

        //迭代

         function setup(x) {
                   var i=0;
                   return function() {
                   	   return x[i++];
                   };
                }

         var next_world=setup([‘a‘,‘b‘,‘c‘,‘d‘,‘e‘]);
         console.log(next_world(),next_world(),next_world(),next_world(),next_world());
     //十六进制值转为颜色函数getRGB()
         function getRGB(hex){
       var rgb=[0,0,0];
        if(/#(..)(..)(..)/g.test(hex)){
        rgb=[parseInt(RegExp.$1,16),parseInt(RegExp.$2,16),parseInt(RegExp.$3,16)];
        };
         return "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>

  

函数的定义

标签:ret   regex   logs   value   argument   dsa   rgb   全局变量   intel   

原文地址:http://www.cnblogs.com/deveil/p/6377850.html

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