【问题标题】:How to pass a bool value from called function back to the calling function如何将布尔值从被调用函数传递回调用函数
【发布时间】:2022-08-17 20:02:49
【问题描述】:

好的,所以我很确定我已经掌握了基础知识,但也许没有,因为我似乎无法理解为什么我的 bar() 函数不会从 foo() 接收更新的 bool 值。有人可以帮助我了解我在哪里出错了吗?

我确定这可能很愚蠢,但是在网上搜索并查看了一些示例之后,我似乎无法完全正确。

预期行为

bar() 应该输出 console.log(\'true\');

当前行为

bar() 当前输出 console.log(\'false\');

提前致谢。

class test {

  constructor() {
    this.bar();
  }

  foo(bool) {
    bool = true;
    return bool; // Return bool with \'true\' value
  }

  bar() {
    let bool = false
    this.foo(bool);
    console.log(bool); // Console the updated value.
  }

}
new test();

    标签: javascript


    【解决方案1】:

    您没有设置返回值。更改您的 bar 函数:

    bar() {
        let bool = false
        bool = this.foo(bool);
        console.log(bool); // Console the updated value.
    }
    

    【讨论】:

    • Fml,?。注意到了,感谢您的帮助。 ??
    猜你喜欢
    • 1970-01-01
    • 2019-12-06
    • 1970-01-01
    • 1970-01-01
    • 2019-02-27
    • 2015-08-24
    • 2013-06-06
    • 2019-03-12
    • 1970-01-01
    相关资源
    最近更新 更多