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

Locating a Method's super

Locating a Method’s super

In this lesson, you find out where a method’s super starts looking.

At the tool library, a kit describes itself for its label, and a tagged kit wraps that text in a tag. In Python, the describe in TaggedKit calls super().describe(), which finds the next class after TaggedKit in the receiver’s MRO, so the receiver decides what comes next:

Language: Python
class Kit:
    def __init__(self, code):
        self.code = code

    def describe(self):
        return f"{self.code}: base"

class TaggedKit(Kit):
    def describe(self):
        return f"tag[{super().describe()}]"

tagged = TaggedKit("KIT-17")
print(tagged.describe())
print([c.__name__ for c in TaggedKit.__mro__])

For a tagged kit, that list runs TaggedKit, Kit, then object, so super() reaches Kit:

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 In this lesson, you’ll find out where a method’s super starts looking. At the tool library, a kit describes itself for its label, and a tagged kit wraps that text in a tag.

00:11 In Python, the describe in TaggedKit calls super().describe(), and Python prints KIT-17’s text inside the tag. Python’s super() finds the next class after TaggedKit in the receiver’s MRO: the receiver decides what comes next.

00:28 Here that list runs TaggedKit, Kit, then object, so super() reaches the Kit class.

00:35 The JavaScript port reads the same and logs the same tagged text. But a JavaScript class has no MRO list: each object links to one parent, and that chain is the lookup order.

00:48 So where does super start?

00:52 In Python, the class’s MRO serves lookups on a tagged kit and on the class itself. JavaScript keeps two chains, and extends builds both.

01:03 The instance chain: a tagged kit links to TaggedKit.prototype, then Kit.prototype, Object.prototype and null.

01:13 Object.getPrototypeOf() confirms the middle link: true.

01:18 The constructor chain starts at the class: TaggedKit links to Kit, then Function.prototype. The link check logs true.

01:28 So a static field on Kit reads on TaggedKit but not on a tagged kit, where a Python class attribute reads on both. TaggedKit.prototype, then, is what instances get, not what TaggedKit links to.

01:42 In the describe of TaggedKit, super.describe() reaches Kit.prototype, one link up from where it was written.

01:52 The window’s label robot runs older code. Its class, Robot, describes a kit with its own text, and Display extends Robot.

02:01 A maintainer wanted tagged labels there too, so they copied the describe from TaggedKit onto Display.prototype. Then a display for KIT-23 calls it.

02:13 this will be the display. Does super follow it up to Robot, as this follows the call? Whose text ends up inside the tag: Robot’s or Kit’s?

02:22 Your turn: pause the video and predict.

02:30 It logs KIT-23 with Kit’s text, base. The display isn’t a kit at all, yet super reached Kit.prototype.

02:42 That call made two lookups. this comes from the call: the display received it, so this.code is KIT-23. super comes from the definition: the method was written on TaggedKit.prototype.

02:56 super starts one link above that object, whatever the receiver. JavaScript calls that object the method’s home object.

03:05 Copying the function didn’t change its home, and .call() or .bind() choose this, never super. Python’s super() also remembers its class, then searches the receiver’s MRO for the class after it.

03:20 JavaScript’s super lookup never looks at the receiver, which only becomes this. Python wouldn’t follow the display to Robot either: the same copy, which nobody would write, would raise a TypeError there, since a display isn’t a tagged kit.

03:36 Python fails loudly; JavaScript quietly reaches the parent of the home object.

03:43 A borrowed method keeps its home, so don’t treat it as a redefinition. Write the override in the class body of Display: the same line, but written there it means what it says.

03:55 Its home is Display.prototype, so super starts at Robot.prototype. The display logs KIT-23 with robot, inside the tag.

04:07 Or, if the window wants a tagged kit’s label, hold a tagged kit as a part and ask it for its text. Either way, the method that calls super lives in the class whose parent it means.

04:20 So keep two facts apart. this comes from the call: whatever object received it. super comes from where the method was written: one link above its home object.

04:31 And each class has two links: its prototype’s, for instances, and its own, for static lookup.

04:39 Next up is the text lesson Initializing Fields in Order. There, you’ll see why moving a label default into a class field skips the label setter, and in what order a derived class’s fields and constructors run.

Become a Member to join the conversation.