Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Composing Cooperative Class Layers

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:

Language: Python
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.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 The checkout desk’s labels wrap each kit’s code in a tag and an audit mark, built in Python by two small mixins. Here, you’ll port them to the JavaScript checkout app.

00:10 In Python, Kit only stores a code, and describe() returns it. Tagged and Audited each wrap what super().describe() gives back.

00:20 LoanKit lists all three as bases, and Python prints KIT-17 wrapped in tag, then in audit. The __mro__ line shows the order: LoanKit, Audited, Tagged, Kit, then object.

00:36 Each super() call moves one step along it. On the right, Kit is already in JavaScript, its constructor storing the code.

00:46 A direct copy moves each Python class across, with describe() calling super.describe(). Then LoanKit extends all three, as in Python.

00:56 Run the file, and it fails before a single line runs: a SyntaxError at the first comma.

01:03 A JavaScript class has a single prototype link, so extends takes one class.

01:10 You could write class Tagged extends Kit, but that welds Tagged to Kit. In Python, super() follows the instance’s MRO when it’s called, so Tagged never names what comes after it.

01:24 In JavaScript, the parent is fixed when the class is built, so pass it in: make Tagged a function. The arrow takes a base class, Base, and returns a class that extends it.

01:36 With no braces of its own, it returns what follows the arrow: here, a whole class. That class is a class expression. Like a function expression, it evaluates to a value: a class.

01:50 Each call runs the expression again, so each call builds a new class.

01:57 Audited gets the same shape, wrapping in audit. extends takes a class name or a call that returns a class, so LoanKit extends the class Audited returns.

02:09 Read the nesting inside out: Tagged wraps Kit, then Audited wraps that. None of these classes declares a constructor, so new passes KIT-17 through each layer to the one in Kit.

02:23 JavaScript logs KIT-17 wrapped in tag, then in audit, as Python does.

02:30 Order matters on both sides. In Python, swap the first two bases: the MRO now puts Tagged first, and Python prints audit inside tag. In JavaScript, swap the nesting so Tagged is the outer call.

02:46 It logs audit inside tag too. So Python’s first base and JavaScript’s outermost call give the outer wrapper. Then the order goes back.

02:58 Here’s the single chain LoanKit builds: kit, LoanKit.prototype, the Audited layer’s prototype, the Tagged layer’s prototype, Kit.prototype, then Object.prototype.

03:11 Python’s MRO lists the classes; JavaScript links their prototypes, in the same order. Each super.describe() starts one link below its home object, the prototype it was written in.

03:25 Python’s MRO algorithm merges every class’s bases, even a diamond, into one list. JavaScript merges nothing: the chain is the nesting you wrote. That’s enough for two small wrappers that each call super once and keep no state.

03:41 It isn’t a general stand-in for Python’s multiple inheritance.

03:47 instanceof asks whether a class’s prototype is on the chain, like Python’s isinstance(). In Python, all three print True, Tagged included.

03:57 In JavaScript, the first line checks LoanKit and Kit: true, true. What does the last line log? Your turn: pause the video and predict.

04:14 It logs false. That call built a new class, outside the chain. A Python def that returned a class would say False too. Python’s Tagged is a class; JavaScript’s is a function that makes classes.

04:31 Compose once, name the result, and check that name. To check for the Tagged layer, name Tagged(Kit) and build LoanKit on it.

04:42 So extends takes one parent. A mixin factory uses a class expression to return a class that extends the base you pass in. Nest the calls to set the order, and name the composed class once.

04:56 Next up is the text lesson Holding a Printer as a Part, where a class holds a label printer instead of inheriting one.

Become a Member to join the conversation.