Composing Cooperative Class Layers
The checkout desk’s labels wrap each kit’s code in a tag and an audit mark, built in Python by two
small mixins. In this lesson, you port them to the JavaScript checkout app. In Python,
the library’s Kit only stores a code, and describe() returns it.
Tagged and Audited each wrap what super().describe() gives back, and LoanKit lists all three as
bases:
class Kit:
def __init__(self, code):
self.code = code
def describe(self):
return self.code
class Tagged:
def describe(self):
return f"tag[{super().describe()}]"
class Audited:
def describe(self):
return f"audit[{super().describe()}]"
class LoanKit(Audited, Tagged, Kit):
pass
kit = LoanKit("KIT-17")
print(kit.describe())
print([cls.__name__ for cls in LoanKit.__mro__])
Python prints audit[tag[KIT-17]], then the order the __mro__ line shows:
['LoanKit', 'Audited', 'Tagged', 'Kit', 'object']. Each super() call moves one step along it.
A direct copy takes each class across and lets LoanKit extend all three:
class LoanKit extends Audited, Tagged, Kit {}. Running the file fails before a single line runs, with
SyntaxError: Unexpected token ',' at the first comma. A JavaScript class has a single prototype link,
so extends takes one class.