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

前端 未结 4 822
你的背包
你的背包 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条回答
  • 2021-01-14 16:07

    The name in function(name) is a parameter. That is, it is used to pass data into the function. But, parameters are local variables. Assigning a value to name inside the function is a little strange though. I would assume you want to do something like this:

     var greeting = function(name) {         
         console.log(name); 
      };
    
      greeting("sausage");
    

    In this version of the code you are passing the value "sausage" into the function via the parameter name. This is useful because you can call the function many times and each time the function may print a different value depending on what you pass.

    0 讨论(0)
  • 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'
    
    0 讨论(0)
  • 2021-01-14 16:23

    In your function definition:

    function(name) {
    

    name is already being declared. It is a parameter for the function. If you want to change name, you can, but you don't need to use var to declare it as a new variable.

    On the other hand, if you wanted to add, say, flavor, you should use var then since it is not already defined.

    var flavor = 'savory';
    

    In this case, flavor is a local variable and name is a parameter. A parameter is a type of local variable that is declared with the function declaration, but a local variable isn't necessarily a parameter because it may be declared elsewhere in the function.

    0 讨论(0)
  • 2021-01-14 16:25

    Parameters are a general programming construct and are necessary to do anything sane in the world programming (dealing with masses of global variables is not sane.

    var name would declare a new variable in the function scope that would override the value of the parameter name, so you would not be able to use the parameter anymore.

    The CodeAcadamy example is a bit strange because it's rare that you want to override a parameter's value -- especially before you have used it.

    0 讨论(0)
提交回复
热议问题