【问题标题】:How can I call a function in the part where this comment in the code?如何在代码中此注释的部分调用函数?
【发布时间】:2017-06-11 20:00:51
【问题描述】:

我有一个问题,我在网上搜索过,但没有找到。

如何在代码中此注释所在的部分调用函数?

if (drawTile != 0) {
    roomTilesCoordinates.push( {
        Coordinate: (i - j) * tileH / 34 + ',' + (i + j) * tileH / 2 / 17,
        ValueCoordinate: CoordinateTilePositionX + ',' + CoordinateTilePositionY,
        PointsCoordinate: //Call Function and return value
    });
}

【问题讨论】:

  • 如果函数在作用域内,并且返回你想要的值,你需要做的就是在注释所在的地方调用它。
  • 但是,问题是如何调用那个部分的函数,我试过正常,但是我不能,我出错了。
  • @ĆarlosOmar 假设您的函数名为“testFunction”并且在同一范围内,您应该可以通过键入“testFunction()”来调用它。

标签: javascript arrays function push


【解决方案1】:
PointsCoordinate: yourFunction()

简单地说,应该可以...

【讨论】:

    【解决方案2】:

    尝试一个 IIFE,一个在计算后立即执行并返回的函数。

    f (drawTile != 0) {
        roomTilesCoordinates.push({
            Coordinate: (i - j) * tileH / 34 + ',' + (i + j) * tileH / 2 / 17,
            ValueCoordinate: CoordinateTilePositionX + ',' + CoordinateTilePositionY,
            PointsCoordinate: (function() {
              // some code
            }()) // notice the () invoking the function
        });
    }
    

    如果您尝试使用现有函数,只需调用它...假设它被命名为 someFunc

    PointsCoordinate: someFunc()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-11
      • 2021-03-26
      • 2011-01-23
      • 1970-01-01
      • 1970-01-01
      • 2016-09-26
      • 1970-01-01
      相关资源
      最近更新 更多