显然在普通的 JS 中我可以做到这一点
var Card = function(rank, suit){
this.rank = rank;
this.suit = suit
}
var cardOne = new Card('3', 'H');
cardOne // Card {rank: "3", suit: "H"}
那么我将如何在 React 和 ES6 土地上做到这一点?
我试过这样的事情:
class ReactApp extends React.Component{
Card = (rank, suit) => {
this.rank = rank;
this.suit = suit;
};
createCard = () => {
let CardObj = {};
let card = new this.Card('3', 'Hearts');
console.log(card);
};
}
(暂时不显示渲染方法)
但是我怎样才能让它在反应中记录正确的事情呢? React 中的函数是如何处理的? (键值对?)以及如何定义对象等?
原文由 The worm 发布,翻译遵循 CC BY-SA 4.0 许可协议
如果您正在寻找 Card 模型,您可以为此创建一个新的 ES6 类
在此之后,您可以将该模型导入到 React 组件中