如何在ES6类中定义静态属性 [英] how to define a static property in the ES6 classes
本文介绍了如何在ES6类中定义静态属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在ES6类中具有静态属性.此属性值最初是一个空数组.
I want to have a static property in an ES6 class. This property value is initially an empty array.
class Game{
constructor(){
// this.cards = [];
}
static cards = [];
}
Game.cards.push(1);
console.log(Game.cards);
我该怎么办?
推荐答案
一种实现方法可能是这样的:
One way of doing it could be like this:
let _cards = [];
class Game{
static get cards() { return _cards; }
}
然后您可以执行以下操作:
Then you can do:
Game.cards.push(1);
console.log(Game.cards);
You can find some useful points in this discussion about including static properties in es6.
这篇关于如何在ES6类中定义静态属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文