gpt4 book ai didi

javascript - 为什么我得到 undefined 加上值?

转载 作者:行者123 更新时间:2023-11-30 11:10:34 25 4
gpt4 key购买 nike

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

当我打电话

budgetController.publicTest2();

我得到:

46 Undefined

我期待 46,但为什么我也得到 Undefined?

完整代码:

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() 表达式的返回值。

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

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

关于javascript - 为什么我得到 undefined 加上值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53892709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com