javascript - 无法理解 console.log 中带/不带闭包的函数行为

标签 javascript function console closures

我对 JS 很陌生。

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

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

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

    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(testClosure(1,2)); 不是吗?

  2. 为什么 console.log(testClosure(1,2)); 不起作用,但是

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

确实有效。

我似乎基本上在做同样的事情:计算给定的参数并打印结果。

我显然缺少的关键区别在哪里?

最佳答案

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

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

关于javascript - 无法理解 console.log 中带/不带闭包的函数行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28561038/

相关文章:

javascript - 通过 AJAX 加载 javascript 元素

javascript - 在加载时显示 Bootstrap 模态

jquery - 单击时更改图像,带有图像映射

c - 任何像一个函数的多个实例

c++ - linux c++ libev 官方示例显示冗余控制台行为

javascript - 鼠标悬停图片有效但对齐?

javascript - jQuery UI 对话框上的日期选择器在第二次打开对话框时失败

Java 不同函数返回类型

ruby-on-rails - 未找到 Ruby gems,但已安装

c++ - 是否可以重载 wcout << 运算符以将其替换为 WriteConsoleW?