我正在寻找JavaScript中与Python装饰器相当的东西(例如@property
),但我不确定如何实现。
class Example:
def __init__(self):
self.count = 0
self.counts = []
for i in range(12):
self.addCount()
def addCount(self):
self.counts.append(self.count)
self.count += 1
@property
def evenCountList(self):
return [x for x in self.counts if x % 2 == 0]
example = Example()
example.evenCountList # [0, 2, 4, 6, 8, 10]
我该如何在JavaScript中实现这个功能?