why go () is not defined? Because anonymous?

var fo = function go(){return 23;}; typeof go(); 

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); 

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()); 

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); 

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); 

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]; } 

    3 answers 3

    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.

      You have assigned a function to a variable, called “Function Expression” (functional expression), so it is not visible from outside this variable.

      They write:

      Function Declaration is a function declared in the main code flow.
      Function Expression - a function declaration in the context of an expression, for example, an assignment.

       var fo = function go(){return 23;}; typeof go();//puk-puk typeof fo();//o yes 

      The names of functions and variables should be different, you assign a value of 1 to a variable, it is no longer a function.

       function myFunction(){ return "Hi"; } var myFunction = 1; var myResult = myFunction(); console.log(myResult); 

      Further, 49 is obtained because you in the function sum1() assigned the value s to global s .

       function sum1(a,b) { s = a + b;// глобальная s=7 return s; } function product(a, b) { var p = a * b; return p; } s = 2; p = 5; p = sum1(s,p); s = product(p,s); // p=7, s=7 document.write("S = " + s + ", P = " + p); 

      Here you var a declared later its use, so its value in the line above undefined.

       function get() { var a = 2; function summ() { var b = 3; b = b + a; var a = 3; return b; } return summ(); } console.log(get()); 
      • one
        oh-oh-oh, Assigning a variable to an anonymous function - no need , but you can - Grundy
      • @Grundy corrected. - Jean-Claude

      why go () is not defined? Because anonymous?

       var fo = function go(){return 23;}; typeof go(); 

      In this case, you use the named functional expression, and the function call by name does not occur, this way of writing the function is used to pass the function to the sending, if in more detail, read here

      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 the second question, read about the operator comma, it returns the last value in this case, the function g1

      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 the third question, I think you are using a named function that does not have access to the variables of the external function, but this is my personal assumption.

      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); 

      Fourth, as you call functions, the values ​​of s and p change, because you use global variables in functions, so they change.

      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); 

      Fifth, you simply change the value from a function to a number, the variable and the function should be named differently.

      I can’t help with the sixth search. Use the fact that it is convenient.

      • Perhaps it is worth copying questions in response, and then the first, fifth, etc. It’s problematic to relate to a specific question, after all there are many questions and every time there’s not much to turn the page here. - Grundy