美文网首页
2-3.函数与作用域

2-3.函数与作用域

作者: guidetheorient | 来源:发表于2017-05-16 06:38 被阅读0次

    1.函数声明和函数表达式有什么区别?

    //函数声明,声明会提升到作用域头部,所以不需要放在调用前
    function a(){}
    
    //函数表达式相当于声明一个变量,变量赋值为函数表达式,必须放在调用的前面
    var a = function(){};
    

    2.什么是变量的声明前置?什么是函数的声明前置?

    就是var变量和function函数的声明会提升到作用域头部

    console.log(a)    //undefined,即将var a提升到了头部,此时未赋值
    var a = 1
    console.log(a)    //1
    fn()              //haha  function fn(){……}声明提升到了头部,所以此处可以调用
    function fn(){
      console.log('haha')
    }
        
    

    3.arguments 是什么?

    用于函数内部的类数组对象,通过arguments对象获取到该函数的所有传入参数;可以像数组一样通过下标得到对应值,也有length属性,但不能使用数组的一些方法属性

    function printPersonInfo(name, age, sex){
      console.log(name);           //xiaoming
      console.log(age);            //10
      console.log(sex);            //male
      console.log(arguments);      //["xiaoming",10,"male"]
      console.log(arguments[0]);   //xiaoming  通过下标取值
      console.log(arguments.length); //3,length属性
    }
        
    printPersonInfo('xiaoming',10,'male')
    

    4.函数的"重载"怎样实现?

    通过函数内if判断,不同输入对应不同执行内容。

    //1
    function printPeopleInfo(name,age,sex){
      i = arguments.length
      if(i === 1){
        console.log(name);
      } else if (i === 2){
        console.log(name)
        console.log(age)
      } else if(i === 3){
        console.log(name)
        console.log(age)
        console.log(sex)
      }
    }
    
    printPeopleInfo('Byron', 26);
    printPeopleInfo('Byron', 26, 'male');
    
    //2
    function printPeopleInfo(name, age, sex){
        if(name){
          console.log(name);
        }
    
        if(age){
          console.log(age);
        }
    
        if(sex){
          console.log(sex);
        }
      }
    printPeopleInfo('Byron', 26);
    printPeopleInfo('Byron', 26, 'male');
    

    5.立即执行函数表达式是什么?有什么作用

    就是函数声明后立即执行,此时需要通过加括号等让解释器以表达式的形式处理函数;
    作用:隔离作用域

    (function(){
      var a  = 1;
    })();
    console.log(a); //undefined
    

    6.求n!,用递归来实现

    function fn(n){
      if (parseInt (n,10) === n){
        if (n === 0){
          return 1
        } else if (n < 0){
          console.log('负数')
          return
        } else if(n === 1){
          return 1;
        } else {
          return (n * fn (n-1));
        }
      } else {
        console.log('不是整数')
      }
    }
    

    7.代码输出

        function getInfo(name, age, sex){
            console.log('name:',name);
            console.log('age:', age);
            console.log('sex:', sex);
            console.log(arguments);
            arguments[0] = 'valley';
            console.log('name', name);
        }
    
        getInfo('饥人谷', 2, '男');
        /*
          name:饥人谷
          age:2
          sex:男
          ["饥人谷", 2, "男"]
          name valley
        */
        getInfo('小谷', 3);
        /*
          name:小谷
          age:3
          sex:undefined
          ["小谷",3]
          name valley
        */
        getInfo('男');
        /*
         name:男
         age:undefined
         sex:undefined
         ['男']
         name valley
        */
    

    8.写一个函数,返回参数的平方和

       function sumOfSquares(){
           var sum = 0
           for (var i = 0; i < arguments.length; i++) {
             var a = arguments[i]
             sum += a*a
           }
           return sum
       }
       var result = sumOfSquares(2,3,4)
       var result2 = sumOfSquares(1,3)
       console.log(result)  //29
       console.log(result2)  //10
    

    9.

        console.log(a);  //undefined
        var a = 1;
        console.log(b);  //Uncaught ReferenceError: b is not defined
    
       /*
        var a
        console.log(a)   //a因为声明前置,但未赋值,所以输出undefined
        a = 1
        console.log(b)   //b未声明,报错
       */
    

    10.

    sayName('world');  //hello world  
    sayAge(10);        //Uncaught TypeError: sayAge is not a function
                          //sayAge使用函数表达式声明,相当于先var sayAge此时还未赋值,所以报错
    function sayName(name){    //使用function关键字声明,声明会提升
       console.log('hello ', name);
    }
    var sayAge = function(age){  
       console.log(age);
    };
    

    11.

      var x = 10
      bar()     //10
      function foo() {
        console.log(x)   
      }
      function bar(){
        var x = 30
        foo()
      }
    
      /*
      1.
      globalContext = {
        AO:{
          x:10
          foo:function
          bar:function
        }
      }
      foo.[[scope]] = globalContext
      bar.[[scope]] = globalContext
      
      2.
      barContext = {
        AO:{
          x:30
        },Scope:globalContext
      }
      
      3.
      fooContext = {
        AO:{},
        Scope:globalContext
      }
      */
    

    12.

      var x = 10;
      bar() 
      function bar(){
        var x = 30;
        function foo(){
          console.log(x)     //30
        }
        foo();
      } 
        
      /*
      1.
      globalContext = {
        AO:{
          x:10
          bar:function
        }
      }
      bar.[[scope]] = globalContext
      
      2.
      barContext = {
        AO:{
          x:30
          foo:function
        },
        Scope:globalContext
      }
      foo.[[scope]] = barContext
      
      3.
      fooContext = {
        AO:{},
        Scope:barContext
      }
      */
    

    13.

      var x = 10;
      bar() 
      function bar(){
        var x = 30;
        (function (){
          console.log(x)         //30
        })()
      }
      
      /*
      1.
      globalContex = {
        AO:{
          x:10
          bar:function
        }
      }
      bar.[[scope]] = globalContext
      
      2.
      barContext = {
        AO:{
          x:30
        },
        Scope:globalContext
      }
      */
    

    14.

    var a = 1;
    
    function fn(){
      console.log(a)          //第一
      var a = 5
      console.log(a)          //第二
      a++
      var a
      fn3()
      fn2()
      console.log(a)          //第五
    
      function fn2(){
        console.log(a)        //第四
        a = 20
      }
    }
    
    function fn3(){
      console.log(a)          //第三
      a = 200
    }
    
    fn()
    console.log(a)            //第六
    
    //undefined 5 1 6 20 200
    
    /*
    1.
    globalContext = {
      AO:{
        a:1 | a:200             //第六:200
        fn:function
        fn3:funciton
      }
    }
    fn.[[scope]] = globalContext
    fn3.[[scope]] = globalContext
    
    2.
    fnContext = {
      AO:{
        a | a:5 | a:6 | a:20  //第一:undefined,第二:5
                              //第五:20
        fn2:funciton
      },
      scope:globalContext
    }
    fn2.[[scope]] = fnContext
    
    3.
    fn3Context = {
      AO:{
                      //第三:1
      },
      Scope:globalContext
    }
    
    4.
    fn2Context = {
      AO:{
                      //第四:6
      },scope:fnContext
    }            
    */
    

    相关文章

      网友评论

          本文标题:2-3.函数与作用域

          本文链接:https://www.haomeiwen.com/subject/kblkxxtx.html