[英]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'),
};
我想出了答案:
只需使用狀態而不是const
只需使用const ,它更全局
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.