why go () is not defined? Because anonymous?
var fo = function go(){return 23;}; typeof go();
Not. In this case, go is not a function definition, but a functional expression. This means that the scope of go limited only by the function itself. Read more in the help.
why does 2 come back? and tell me what is this construction after equal, also a comma between functions?
var f1 = (function f1(){return "1";}, function g1(){return 2;})(); console.log(typeof f1); console.log(f1);
In this case, the function expression is again used, as well as the comma and IIFE operator (when the function is called immediately after the description)
why in the following code we get - NaN?
function get() { var a = 2; function summ() { var b = 3; b = b + a; var a = 3; return b; } return summ(); } console.log(get());
In javascript, there is such a thing as hoisting , that is, the example above can be rewritten as:
function summ() { var a,b; b = 3; b = b + a; a = 3; return b; }
The value of uninitialized variables is undefined , and adding undefined to a number gives NaN .
The following code, why s = 49? I lose the calculation chain (
function sum1(a,b) { s = a + b; return s; } function product(a, b) { var p = a * b; return p; } s = 2; p = 5; p = sum1(s,p); s = product(p,s); document.write("S = " + s + ", P = " + p);
It is worth using the debugger to walk through the steps and see the values of variables.
why the error myFunction is not a function (anonymous) In myResult, the code is passed to f-and without a name, so it is perceived by the variable as anonymous?
function myFunction(){ return "Hi"; } var myFunction = 1; var myResult = myFunction(); console.log(myResult);
Nothing is considered anonymous here . Just when declaring a variable, its name overlaps the name of the function.
In general, there may be interesting results depending on the location of the var , assignment, and attempt to call the function.
What is the most optimal design for enumerating values in an array? To avoid "holivar" I will clarify, optimal regarding performance. If possible, indicate instead of the performance of another characteristic and the optimal design for it.
for(var i=0; i<enumerable.length;i++){ enumerable[i]; } for(var key in enumerable){ enumerable[key]; } var i=enumerable.length; while (i--) { enumerable[i]; }
Generally speaking, the second type: for..in generally not recommended for working with arrays.
The difference between the two remaining ones is rather arbitrary, except that in the first variant the length property is taken at each iteration.
To say which of the implementations is unambiguously more productive is not possible, since it depends on too many conditions, it may turn out that either one or the second can lead in different browsers, or they will show approximately the same result. To find out for a specific computer and browser, you can run a test for example on jsperf
Also, it is possible that the optimizer specifically in the given example can throw out loops altogether, since nothing is done inside them and does not change.