简体   繁体   中英

Are the same variable and parameter names allowed in a javascript function?

As an example am I allowed to use the same variable and parameter? What issues can I run into?

Sample code

function mytask(name,title){
            var name = name;
            var title = title;
            var showalert = ("Hi " + name + " your job title is " + title);
            console.log(showalert);
            return showalert;
        }

document.write(mytask("dan", "administrator"));

Well in javascript you can think that, scopes are defined my curly brackets: { And } , and inside a scope variables can be redefined, so look at:

function x(){
  var name=3;
  var name=4;
  console.log(name); // output is: 4
}
x();

But this is just half true, actually what happens is that the interpreter goes over the code, and moves all var statements to the beginning, while they're assigned an undefined (and all arguments are defined and taken from stack), and then the code you wrote will run. So any var after the first is simply ignored. And the code you wrote is equal to:

function mytask(name,title){
   var name = arguments[0];
   var title = arguments[1];
   name = name;
   title = title;
   var showalert = ("Hi " + name + " your job title is " + title);
   console.log(showalert);
   return showalert;
}

document.write(mytask("dan", "administrator"));

So your re-deceleration, and assignment is redundant. Anyway - the scope is not changing, nothing else will differ.

Edit

The interpreter goes over your code, with executing anything, any var x = y; statement will split into var x = undefined; and x=y; . And the var x = undefined; will be moved to the top of the code. And the x=y; will be at the same place as the original statement. If you didn't understand the stuff about the stack, don't bother, that's how compilers convert function calls to assembly - it's worth knowing in case you have time; but not THE important thing here.

Anyway - just after those changes, and maybe some optimizations are made, the resulting code is executed. This is not the code you wrote, but an equal one. What you pointed out in redefining the arguments is an edge case where this transformations become visible.

Think about it this way:

var name = name;

The only way name can be set to the value of name is if name is already defined. There's no need to do it twice if name already has the value you want.

Well, I suppose some explanation won't hurt. )

First, all the function's params are already declared as local for this function. (it's a bit more complex, but let's say this explanation covers most of it). So it's really no use defining them again: they won't become 'more local' after it. ))

Second, it's possible to write var name = name within the function(name) for the same reasons why it's possible to write var name = 123; var name = 345; var name = 123; var name = 345; . The second var will be just silently ignored.

And if you write var name = 123; var name = name; var name = 123; var name = name; , you'll just waste some keystrokes - as, again, it's the same as you write name = name; somewhere in your code. )

And that, btw, explains messing up the arguments . See, name is actually an alias for its element.

Sure you can run into problems. Take a look at this.

function mytask(name,title){
            console.log(name);
            console.log(title)
            var name = "oops";
            var title = "rawr";
            console.log(name);
            console.log(title)
}

mytask("dan", "administrator");

Apart from being very confusing, calling console.log(name); gives us two different results (because we redefined it). It's allowed, but it's not a good idea to do.

Edit : Interestingly (I didn't know this), doing the above screws up the implicit arguments[] array as well. For example, doing:

for( var i = 0; i < arguments.length; ++i ) {
    console.log(arguments[i]);
}

somewhere inside our function (post-reassignment) still outputs oops and rawr . Obviously, this is a bad, bad idea.

您可以使用与参数同名的变量,因为无论如何其值都是相同的,除非同名的变量与参数具有不同的值。

Actually there's no reason to do it. Once you pass them to the function, they are initialized and you can use them without reassigning them to other variables.


<script>

    //this function returns what you give as argument
    function func1(name1) {
        let name1 = "mess shahadat"; //can't declare name, cause already getting name as parameter. thus this gives error if you define a variable same name as parameter with let. cause you can't redeclare let variable in javascript
    }

    document.write(func1("mike"))


</script>

in javascript, function parameters works like local variables. inside function, if you declare variable with var keyword and use same name as parameter, then it will return undefined. and if you declare the variable with let keyword, you will get an error. cause let variables are not redeclarable.

so don't use the same variable name as parameter when you need the variable with the same name to have a different value than the parameter, cause unknownly it can become a bug in your project.

example:

<script>

    //constructor function
    let mobile = function (modelNo, ram, price) {
        this.model = modelNo;
        this.ram = ram;
        let price = price + 1000; //can't declare price, cause already getting price as parameter. and let variables are not redeclarable
        this.price2 = function () { return price };
        this.totalPrice = function () { return "total price is: " + this.price2 }
    }

    let samsung = new mobile("samsung dous", "2gb", 3000);

    document.write(samsung.price2());

</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM