为什么我得到 undefined 加上值?

Why do I get undefined plus the value?

我是 JS 的新手,我只是在玩弄语法。

当我打电话时

budgetController.publicTest2();

我得到:

46 Undefined

我期待 46,但为什么我也得到未定义?

完整代码:

var budgetController = (function() {
    var x = 23;

    function add(a) {
        return a + x;
    }

    return {
        publicTest: function() {
            const y = add(23);
            return y;
        },
        publicTest2: function() {
            return (function(d){
                console.log(d());
            })(budgetController.publicTest);
        },
    }
    })();
    
budgetController.publicTest2();

控制台打印出 undefined,因为这是 budgetController.publicTest2() 表达式的 return 值。

控制台始终打印出您输入的表达式的值。例如:

  • 如果您输入 1 + 2,您将返回 3
  • 如果你输入 console.log(3) 您将返回 3undefined(return 表达式的值 console.log(3))