What is the different between a parameter and a local variable?

前端 未结 4 852
你的背包
你的背包 2021-01-14 16:06

Apologies for what must seem like a very stupid question.

I\'m currently working through codecadamy, and this is throwing me off:

 var greet         


        
4条回答
  •  旧时难觅i
    2021-01-14 16:07

    Technically, there is no real difference.

    Without giving you the huge background here, you have to understand that in the underlaying implementation, a special object (not a javascript object, on C/C++ level) is formed which is called Activation Object (ES3) or Lexical Environment Record (ES5).

    However, this hash / object structure is used to store

    • variables declared by var
    • formal parameters
    • function declarations

    As you can see, both var variables and parameters are stored in this structure.

    This construct is most likely used to have somewhat default values for not passed in arguments. In a real world example, this would probably look more like

    var greeting = function( name ) {
        name = name || 'default';
    
        console.log( name );
    };
    
    greeting('john');  // 'john'
    greeting();        // 'default'
    

提交回复
热议问题