无法理解 console.log 中的函数行为 with/out 闭包

Fail to understand function behavior with/out closures in console.log

我对 JS 很陌生。

我 运行 进入了 st运行ge,同时了解 console.log 行为。控制台对我来说非常重要,因为我相信如果 console.log 没有看到它,那么它就不存在。我将从简单的事情开始,只是为了确保我不会错过任何事情。

  1. 假设:console.log可以打印简单计算的结果。

    console.log(1+0); //1. Console.log can calculate!
    
  2. 假设:console.log 可以打印函数的结果,如果那些 return 值。

    var test = function(a,b){
        var c = a+b; 
        return c; 
    };
    
    console.log(test); //prints the assigned value of the variable "test", which happens to be a function and thus function code is printed. Function is NOT called here.
    
    console.log(test()); //prints NaN, since the function is called but no arguments are provided. Thus, JS is unable to calculate the result.
    
    console.log(test(1,1)); // this one prints the result of the function, which is variable "c"
    
    console.log(test(1,1) + 2); //I can then manipulate results further in the console.
    
  3. 假设:闭包有问题?

    var testClosure = function(a,b){
        var c = a+b;
        return function(){
            var d = c + 1;
            return d;
         }
     };
    
    console.log(testClosure); //so far standard behavior - console prints the assigned value of a function - its code.
    
    console.log(testClosure()); //trying to call the function here without arguments. Expected result that it will attempt to calculate "d" and return NaN (since it cannot calculate), but it returns the code for the anonymous function. JS does not attempt to do calculation.
    
    console.log(testClosure(1,2)); //does not produce the expected result of 4. It produces same output as in the previous case. JS does not attempt to do calculation for some reason.
    
    var result = testClosure(1,2);// Do I understand it correctly that at this point I only create a reference but testClosure() function is not actually launched? Therefore, variable "result" does not have any meaningful value yet?
    
    console.log(result); //printing the assigned value of this variable, which is the code for the anonimous function in testClosure().
    
    console.log(result()); // here I am actually calling the testClosure() function by proxy and get the desired result of 4.
    

我的主要问题是

  1. 为什么

    console.log(test(1,1)); 
    

    工作和 console.log(测试闭包(1,2)); 没有?

  2. 为什么 console.log(测试闭包(1,2)); 不工作但是

    var result = testClosure(1,2);
    console.log(result());
    

有效。

我似乎在做本质上相同的事情:根据提供的参数进行计算并打印结果。

我显然缺少的主要区别在哪里?

1) 因为您正在记录执行函数的结果,returns 预期值。

2) 因为你正在记录结果,在本例中是一个函数,你需要执行这个函数以获得所需的值 - 尝试做 console.log(testClosure(1, 2)());

test 是一个函数,return 是一个值,但是 testClosure 是一个函数,return 是一个函数,return 是一个值。 JS 中的函数是第一个 class 对象,这意味着它也可以用作参数,分配给变量或从函数 returned (这就是你在闭包示例中所做的,你return 一个函数,而不是值)。但是要获得此 returned 函数的值,您也必须调用它,它不会调用自身。

console.log( typeof test(1,2) ); // number

console.log( typeof testClosure(1,2) ); // function, which also needs to be called

console.log( typeof testClosure(1,2)() ); // number
// ---------------------------------^-----// call the returned function