闭包

作者: 小7丁 | 来源:发表于2018-08-02 17:26 被阅读7次

    一、如下代码输出多少?如果想输出3,那如何改造代码?

    var fnArr = [];
    for (var i = 0; i < 10; i ++) {
      fnArr[i] =  function(){
        return i
      };
    }
    console.log( fnArr[3]() ) 
    

    10

    //方法一
    var fnArr = []
    for(var i = 0; i < 10; i++){
        fnArr[i] = (function(j){
          return function(){
            return j
          }
        })(i)
    }
    console.log( fnArr[3]() )
    //方法二
    var fnArr = []
    for(var i = 0; i < 10; i++){
      (function(i){
        fnArr[i] = function(){
          return i
        }
      })(i)   
    }
    console.log( fnArr[3]() )
    //方法三
    var fnArr = [];
    for (let i = 0; i < 10; i ++) {
      fnArr[i] =  function(){
        return i
      };
    }
    console.log( fnArr[3]() )
    

    二、封装一个 Car 对象。

    var Car = (function(){
       var speed = 0;
       function setSpeed(s){
         speed = s
       }
       function get(){
         return speed
       }
      function speedUp(){
        return speed++
      }
      function speedDown(){
        return speed--
      }
       return {
          setSpeed: setSpeed,
          get: get,
          speedUp: speedUp,
          speedDown: speedDown
       }
    })()
    Car.setSpeed(30)
    console.log(Car.get()) //30
    Car.speedUp()
    console.log(Car.get()) //31
    Car.speedDown()
    console.log(Car.get())  //30
    

    三、如下代码输出多少?如何连续输出 0,1,2,3,4?

    for(var i=0; i<5; i++){
      setTimeout(function(){
        console.log('delayer:' + i )
      }, 0)
    }
    
    //方法一
    for(var i=0; i<5; i++){
      (function(j){
        setTimeout(function(){
        console.log('delayer:' + j )
        }, 0)
      })(i)
    }
    //方法二
    for(var i=0; i<5; i++){
      setTimeout((function(j){
        return function(){
        console.log('delayer:' + j )
        }
      })(i),0)
    }
    

    四、如下代码输出多少?

    function makeCounter() {
      var count = 0
    
      return function() {
        return count++
      };
    }
    
    var counter = makeCounter()
    var counter2 = makeCounter();
    
    console.log( counter() ) // 0
    console.log( counter() ) // 1
    
    console.log( counter2() ) // ?
    console.log( counter2() ) // ?
    

    0
    1
    0
    1

    有两个不同的作用域,对象是不一样的

    五、补全代码,实现数组按姓名、年纪、任意字段排序。

    var users = [
      { name: "John", age: 20, company: "Baidu" },
      { name: "Pete", age: 18, company: "Alibaba" },
      { name: "Ann", age: 19, company: "Tecent" }
    ]
    
    users.sort(byField('age'))
    users.sort(byField('company'))
    
    var users = [
      { name: "John", age: 20, company: "Baidu" },
      { name: "Pete", age: 18, company: "Alibaba" },
      { name: "Ann", age: 19, company: "Tecent" }
    ]
    function byField(field){
      return function(a, b){
        return a[field] > b[field]
      }
    }
    users.sort(byField('company'))
    users.sort(byField('age'))
    

    六、写一个 sum 函数,实现如下调用方式。

    console.log( sum(1)(2) ) // 3
    console.log( sum(5)(-1) ) // 4
    
    function sum(a){
        return fuction(b){
            return a + b
        }
    }
    

    相关文章

      网友评论

        本文标题:闭包

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