What is correct: var declaration if undefined

I heard different opinions about using var in this situation:

 function(maybeUndefined) { if(typeof maybeUndefined === 'undefined') var maybeUndefined = 'bob'; } 

Should var be denoted or not, because maybeUndefined is the argument of function ?

+4
source share
4 answers

In this case, you do not need var , since mayBeUndefined already allocated within the function (hint: listing the argument variables in the function definition causes these variables to be declared locally). Thus, var is completely optional, although completely pointless (and a leak on readability).

+10
source

Example:

 function func ( arg1, arg2 ) { var local1, local2; function nested1 () {} function nested2 () {} // other code } 

Here we have a function declaration. When this declaration is parsed into a function object, a lexical environment (= area) is created for this function with the following bindings:

  • arg1
  • arg2
  • local1
  • local2
  • nested1
  • nested2
  • this is
  • the arguments

(Note that there are also two special built-in bindings: this and arguments . They are always created for all function objects.)

These names are defined as local bindings. (This process is listed in “Binding Binding Application . Warning: This algorithm is not intended for people to read :-) ). Therefore, when a name is defined as a parameter, there is no need to declare it as a local variable. This mechanism does not depend on whether the value (argument) is passed for this parameter when the function is called.

So, even if you call a function like this:

 func(123); 

the arg2 name will still be defined (as a binding in a functional environment), although its value will initially be undefined for this particular call.

Btw, if you use a strict language (recommended!), The function environments are static , which means that the specified bindings are guaranteed to be the only bindings in the function environment. On the other hand, the default language provides certain mechanisms that dynamically add / remove bindings from the environment of functions. Example:

 (function () { // the name "temp" does not exist as a binding in the function environment eval('var temp'); // now it does delete temp; // and it gone again }()); 
+6
source

You should not use var again, this is bad for readability, and the variable will already be locally local as a result of the argument.

In addition, you should note that it is not part of this . this will only be bound to the function object if the new keyword was used, and since you do not have a named function, this is unlikely in this case. Without new, this refers to a window (or undefined if use strict; ), from which your variable is definitely not part of an argument that has a local scope.

+3
source

Pairing

The inclusion of a function argument actually matches the definition of the scope of the variable (in other words, it is actually the same as defining a link at the function level using the var keyword). The main reason for providing function arguments (in JavaScript) is for your own interface.

arguments object

Arguments can still be passed to functions without parameters and will still be available in the 'hidden' arguments object, which is a kind of “pseudo-array” (if you want), because it is a functionally array, but not equipped with the same APIs JavaScript is equipped with Array (pseudo-type) interfaces:

 // The following functions do the same thing, but one is "more readable" function foo() { return arguments; } function bar(baz, qux) { return arguments; } 

Evaluation (interface) vs Execution (implementation)

When both functions are evaluated (in the "load" file), the arguments object is undefined in each function definition; the object does not become “defined” until the function body executes the code in it; to visualize what using pseudo-code it would look something like this:

 // Function bodies (as objects) foo : { arguments : new Array // [undefined] __proto__ : Empty() // the super-object that allows this object to inherit "functionality" } bar : { arguments : new Array(baz, qux) // [undefined, undefined] __proto__ : Empty() } 

Function call

Therefore, when you call a function, it "implements" or "executes" its body (its "object"). When this happens, if the objects that were placed in the arguments object are defined, the function can refer to them. If not, a reference error will be selected that registers undefined variables in this area.

In short:

There is no need to bind scope level variables (like "private members") with var , because the language already attaches the arguments object to the whole body of function objects.

Additional Information:

Jamming JavaScript: A "caching function" of several arguments for better performance: http://decodize.com/javascript/javascript-memoization-caching-results-for-better-performance/

+1
source

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


All Articles