简体   繁体   English

在const内部定义函数

[英]Define function inside const react native

I typed the code below in react-native and it returned the following error 我在react-native中输入以下代码,并返回以下错误

undefined is not a function (evaluating 'this.createPiece('T')') 未定义不是函数(evaluating 'this.createPiece('T')')

I don't know how to define it. 我不知道如何定义它。 Please help me 请帮我

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

and the createPiece method: 和createPiece方法:

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

full code 完整的代码

Try this : 尝试这个 :

  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'),
    };

I figured out the answer: 我想出了答案:

  • React Native: React Native:

Just use state instead of const 只需使用状态而不是const

  • React: 阵营:

Just use const , it more global 只需使用const ,它更全局

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM