【问题标题】:Define function inside const react native在 const react native 中定义函数
【发布时间】:2019-02-01 09:18:07
【问题描述】:

我在 react-native 中输入了下面的代码,它返回了以下错误

undefined 不是函数(evaluating 'this.createPiece('T')')

我不知道如何定义它。请帮帮我

    const player = {
    pos: {x: 5, y: 5},
    matrix: this.createPiece('T'),
};

还有 createPiece 方法:

createPiece(type) {
    if (type == 'T') {
        return [
            [0, 0, 0],
            [1, 1, 1],
            [0, 1, 0],
        ];
    }
}

full code

【问题讨论】:

  • 不确定这是否是您的实际代码,但如果是,则无效。例如函数声明无效
  • 那怎么申报呢??这是我的问题吧??
  • 它可能需要在其名称前加上function 关键字(如果它不是类方法)。此外,它不会为所有输入返回值,仅适用于 'T'
  • 你能发布整个课程吗?为了提供帮助,我们需要查看您的功能范围。
  • 请发布您的整个代码。 const player 初始化位于何处,该方法定义在哪个对象上?

标签: javascript function react-native methods constants


【解决方案1】:

试试这个:

  this.createPiece = function (type) {
        if (type== 'T') {
            return [
                [0, 0, 0],
                [1, 1, 1],
                [0, 1, 0],
            ];
        }
    };
    const player = {
        pos: {x: 5, y: 5},
        matrix: this.createPiece('T'),
    };

【讨论】:

  • 它仍然错误,与我的代码相同。尝试查看完整代码(正如我在上面的帖子中编辑的那样)
【解决方案2】:

我想出了答案:

  • 反应原生:

只需使用 state 而不是 const

  • 反应:

只需使用 const,它更全局

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    • 2019-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-08
    相关资源
    最近更新 更多