簡體   English   中英

在const內部定義函數

[英]Define function inside const react native

我在react-native中輸入以下代碼,並返回以下錯誤

未定義不是函數(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],
        ];
    }
}

完整的代碼

嘗試這個 :

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

我想出了答案:

  • React Native:

只需使用狀態而不是const

  • 陣營:

只需使用const ,它更全局

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM