函数面试题

一,作用域,作用域链

<script type="text/javascript">
	   var a = 20;
	   function arr(){
	      var a = 10;
	      var b = 50;
	      function arr2(){
	        var c = 25;
	        console.log(a);//10
	        //这个a是在arr执行后,在作用域里面找,因为arrAO里面有a ,所以 a  = 10
	        console.log(b);//50
	        console.log(c);//25
	        return arr;
	      }
	      arr2();
	      console.log(a);//10
	      //这个a是找的也是arrAO的a ,所以 a  = 10
	   }
	   console.log(a);//20
	   //这个a是直接找作用域GO里面的a;因为此时arr函数未执行,所以 a  = 20
	   arr();
	   // GO{
    //        a : 20
    //        arr : fn...
	   // }
	   // arr AO{
	   // 	  a : 10;
	   // 	  b : 50;
	   // 	  arr2 : fn...;
	   // }
	   // arr2 AO{
	   // 	  c : 25;
	   // }
	</script>
<script type="text/javascript">
	var name = 'll';
	var age = 10;
	var sex = '男';
	function work(){
		var age = 18;
		console.log(name);//ll
		console.log(age);//18
		console.log(sex);//男
		return work;
		function work2(){
			var sex = '女';
			console.log(name);
			console.log(age);
			console.log(sex);
		}
		work2();
	}
	work();
	console.log(name);//ll
    console.log(age);//10
    console.log(sex);//男
    // GO{
    // 	name : 11
    // 	age : 18;
    // 	sex : 男;
    // 	work : fn...
    // }
    // work AO{
    // 	age : 10;
    // 	work2 : fn...; 
    // }
    // work2 AO{
    // 	sex : 女;
    // }
	</script>
<script type="text/javascript">
	+function(){
        console.log(a)// fn....  函数和和变量先执行,
        var a=5;  //原地赋值为5 a = 5
        function a(){}
        console.log(a) //5    
        function b(){}
        b=6 //b赋值为6 , 为全局变量
        console.log(b) //6 
        var c=d=b  // d和b都为全局变量 所以都是6
    }()
    console.log(d) // 6
	</script>
<script type="text/javascript">
	console.log(pom)  // 变量声明提前 此时为undefined       
   var pom=0;
   function fn(num1,num2){
    console.log(pom) // 变量声明提前 此时为undefined       
   var pom=num1+num2
   console.log(pom)  // pom= num1 + num2 100+200 =300    
  }
  fn(100,200) // 运行函数
  console.log(pom)  // 0       
	</script>

二,预编译

<script type="text/javascript">
	 function test(a,b){
		  //预编译函数和变量的声明提前
		    // 所以函数a提起声明
		    // 变量提前声明但未赋值
          console.log(a); //fn....
          console.log(b); //undefined
          var  b =10;
          // 函数运行到此处,给b赋值为10
          console.log(b); //10 
//           此处给a声明没有用var关键字声明,所以此处a为全局变量 GO a = 20
          a=20;            
          console.log(a); //20
          function a(){}  
//           此处再次给a声明 并用var关键字声明,b也声明,全局变量 GO b = 30
          var a; 
          b=30; 
          // 此处给b赋值,赋值为一个函数体 A0 b fn....        
          var b = function(){}
          console.log(a); //20
          console.log(b); //fn.... 
         }
     test(1);

	</script>
<script type="text/javascript">
	 //预编译函数和变量的声明提前
		    // 所以函数a,b提起声明
		    // 变量提前声明但未赋值
	 console.log(a);//undefined
	 console.log(b);//undefined
	 //声明的变量a 和 b 是局部变量 c 是全局变量 b和c都赋值为10
	 var a ; 
	 var b = c = 10;
	 function arr(){
     console.log(c); // 10
     // 因为b没有var关键字声明,所以声明不会提前,此时的b声明为全局变量
     b = function(){};
     //此时a声明成全局变量,赋值为20
     a = 20;
     console.log(a);//20
	 console.log(b);//fn....
	 console.log(c);//10
	 }
	 arr();
	</script>
<script type="text/javascript">
   var x = 1, y = z = 0; 
   // x有var关键字提前声明,赋值为1	 
   function add(n) {  
      return n = n + 1;  
  }  
   y = add(x);
   
   function add(n) {  
      return n = n + 3;  
   }  
   z = add(x);
   //因为预编译函数会提起声明,下面的函数把上面的函数覆盖
   // 将x值代入到add函数中去,此时y值等于返回的x+3    
   // 将x值代入到add函数中去,此时z值等于返回的x+3    
   console.log(x,y,z)//1,4,4

	</script>
 <script type="text/javascript">
  	function test (a,b){
  	//没有var关键字声明,a由参数a为1
    console.log(a) // 1 
    c = 0;
    var c;提前声明,赋值留在原地
    a = 3;
    b =2;
    //c d函数 b函数提前声明,然后c a b 再次赋值,所以b为2
    console.log(b); // 2
    function b() {}
    function d() {}
    console.log(b)  // 2
 }
 test(1)
  </script>

三,闭包

<script type="text/javascript">
	function f1(){
        var b = "闭包";
        function f2(){
            alert(b);
        }
        return f2;
       通过调用f1函数里面的f2函数得出b的值
       因为f2函数里面没有b的值,继续向父级找,直到找到为止
    }
    var f = f1();
    f();  //弹出:闭包	
	</script>
<script type="text/javascript">
	function f1(num){
        function f2(){
            alert(num);
        }
        return f2;
    }
    通过调用f1函数中的f2函数
    var fa = f1(10);
    执行,参数为10
    f2函数找不到,继续向父级找,得10
    var fb = f1(20);
     执行,参数为20
    f2函数找不到,继续向父级找,得20
    var fc = f1(30);
     执行,参数为30
    f2函数找不到,继续向父级找,得30
    fa();   //10
    fb();   //20
    fc();   //30	
	</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值