4
我正在尋找相當於JavaScript的Python裝飾器(即@property
),但我不知道該怎麼做。JavaScript的Python裝飾器
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呢?
還有get和set語法可以在聲明對象字面值時進行內聯,如果您希望它更接近內聯的@property:https://developer.mozilla.org/en-US/docs/Web/的JavaScript /參考/運營/獲取 – slebetman