Self executing function passing object after condition

前端 未结 2 1514
广开言路
广开言路 2021-01-23 05:51

I have come across a self executing function that executes on a condition that the declared containing var exists, and if it doesn\'t exist it is passed an object.

Exam

2条回答
  •  清酒与你
    2021-01-23 05:58

    var myFunc = (function(myFunc){}(myFunc||{}));
    

    This doesn't make any sense because the myFunc Argument will always be {} - I'm confused by that.

    Ill explain one that does

    First Example

    var cool = {
       person: 'john'
    };
    
    (function( Argument ){
      console.log( Argument ); // Result Object {person: "john"} 
    }( cool || {} ));
    

    In this example cool is defined and is a object so it wont go past the ||


    Next example

    var cool;
    (function( Argument ){
      console.log( Argument ); // Result Object {}
    }( cool || {} ));
    

    In this example cool is defined But the defualt value for a variable is undefined so in this case it is undefined so Argument is a Object instead

提交回复
热议问题