简体   繁体   中英

re-declaring arguments variable doesn't throw any error - JavaScript

Reading the docs about the arguments object at mdn .

The arguments object is a local variable available within all non-arrow functions. You can refer to a function's arguments inside that function by using its arguments object.

It says the arguments is a local variable , but if I re-declare it using let/const , no error is thrown, unlike the ordinary ones.

function fn(arg1) {
  // let arg1 = []; // Uncaught SyntaxError: Identifier 'arg1' has already been declared
  let arguments = []; // array or whatever, no error
}

So my question is why doesn't re-declaring arguments variable throw any error?

In strict mode, it is a syntax error to use arguments (or eval ) as a variable name:

function fn(arg1) {
  "use strict";
  let arguments;
}

In sloppy mode, you're just shadowing replacing the implicit variable with a let , function , or parameter declaration. If you create your own variable with the name arguments , the Arguments object is not created at all so that it doesn't collide with your code. It's not a re-declaration of a var arguments - the implicit arguments variable is created conditionally.

Still, if you explicitly write var arguments , that doesn't count to prevent the implicit creation, but doesn't overwrite it either.

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