Function Arguments Passing and Return

前端 未结 4 1910
无人及你
无人及你 2021-01-25 06:37
  var foo = {
    bar: function() { return this.baz; },
    baz: 1
  };
  (function(){
    return typeof arguments[0]();
  })(foo.bar);

Why does this c

4条回答
  •  隐瞒了意图╮
    2021-01-25 07:02

    You can reffering to foo by this statement.

    You should write a wrapper like below which will very helpfull if will we wanted to change the name of the foo when we will have a lot of function in foo

    var foo = {
        bar: function() { return this.baz; },
        baz: 1
      };
    
    (function(a){
     for( var i in a){ 
        if( typeof a[i] === "function" ){
    
          a[i] = (function(f){  
                return function(){
                    return f.apply(a,arguments);
                }
          })(a[i])
        }
     }
    })(foo);
    
      (function(){
        return typeof arguments[0]();
      })(foo.bar);
    

提交回复
热议问题