Access to a function when a name is overwritten with an argument

Typically, a function can access it as follows:

(function f() { console.log(f); // Prints the function definition }()); 

However, when the function f has an argument, also called f , the argument takes precedence:

 (function f(f) { console.log(f); // Prints 1 }(1)); 

In the second example, how can I access a function when one of the arguments has the same name as the function?

[Also, where can I find documentation that the argument should take precedence over the function name?]

+4
source share
3 answers

No. Just follow this simple rule.

Do not obscure (or override in a more specific area) any variable that you want to use.

Note: arguments.callee will work for this, but only in some implants. And it actually stops and is likely to disappear completely in the future.

+4
source

Regarding

[Also, where can I find the documentation that says that an argument should take precedence over a function name?]

JavaScript is lexically / statically copied . The following code contains two identifiers:

 (function f(f) { console.log(f); // Prints 1 }(1)); 
  • A function named f in the global scope (available with window.f )
  • A parameter of a global function named f , which itself is called f . This parameter is bound to a function, which is a more specific area than the outer area. In other words, no special rule is required for parameter priority; local / internal / more specific scope always masks external / less specific areas.
+1
source

you can use arguments.callee inside a function without using its name.

 ( function f( f ) { console.log( f ); // Prints 1 console.log( arguments.callee ); // Prints the function definition }( 1 ) ); 
0
source

Source: https://habr.com/ru/post/1385804/


All Articles