How is a closure different from a callback?

前端 未结 9 1078
伪装坚强ぢ
伪装坚强ぢ 2020-12-07 19:08

I asked a question about callbacks and arrived at another question (see comment). How is a closure different from a callback?

9条回答
  •  心在旅途
    2020-12-07 19:50

    closure :

    • A function keyword inside another function, you are creating a closure

    • Or A function return to an other function we can say closure

    Note Plain English : A little bit difference function passing as argument in another function is callback or if define in another function is closure

    var length = 101;
    function fn2() {
    	console.log("fffxxx: "+this.length);
    }
     
    var obj = {
      length: 5,
      method3: function(fn) {
        fn();
        arguments[0]();
      }
    };
     
    obj.method3(fn2, 1);
       
    **Outputहोगा

    fffxxx:101
    fffxxx:2**
    

提交回复
热议问题