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

Porting the Loan Kit Model

Porting the Loan Kit Model

You ported the Northbank Tool Library’s loan kit model to JavaScript. In Python, each kit has a code, a trimmed label and its own list of tools. A property validates the label, __iter__ lets you loop over the tools as often as you like, and two cooperative mixins wrap the description: one tags it, and one audits it.

The Python script prints five lines: each kit’s description and tools, the drilling kit’s tools twice. The port must match them. One line won’t map directly: LoanKit has three bases, and extends takes one. The label printer and tool count wait for the exercise.

Python’s class attribute becomes a static field, so Kit.kind reads tool kit. A Python kit reads tool kit too, but a JavaScript kit reads undefined: its lookup climbs prototypes, never the class, so it can’t reach a static field. The port never needs it there.

The constructor stores the code and the label, and spread copies the tools, as list() does, so each kit owns its list. The property becomes a getter and a setter that trims the text into _label. Assigning this.label in the constructor, not declaring a field, runs the setter for every 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 Here, you’ll port Northbank’s loan kit model to JavaScript. In Python, each kit has a code, a trimmed label and its own list of tools. A property validates the label, and __iter__ lets you loop over the tools as often as you like.

00:15 Two cooperative mixins wrap the description, in a tag and an audit. The script prints five lines: each kit’s description and tools, the drilling kit’s tools twice.

00:26 Your port must match them. One line won’t map directly: LoanKit has three bases, and extends takes one. The label printer and tool count wait for the exercise.

00:39 Python’s class attribute becomes a static field, so Kit.kind reads tool kit. A Python kit reads tool kit too. What does a JavaScript kit read?

00:50 Your turn: pause the video and predict.

00:58 undefined. A kit’s lookup climbs prototypes, never the class, so it can’t reach a static field. The port never needs it there.

01:09 The constructor stores the code and the label. Spread copies the tools, as list() does, so each kit owns its list. The property becomes a getter and a setter that trims the text into _label.

01:23 Assigning this.label in the constructor, not declaring a field, runs the setter for every kit.

01:30 describe builds its text with a template literal, not an f-string. Saved in a variable, Python’s drilling.describe stays bound, but JavaScript’s fails when called: no this.

01:43 __iter__ becomes a method under the symbol key Symbol.iterator, in square brackets. It returns this.tools.values(): a fresh iterator per call, like iter(), never the array.

01:56 The last member has no Python line. Python’s default string names the class, but a JavaScript kit in a string reads object Object. A getter under Symbol.toStringTag changes that: with no conversion hook, String() falls back to the tag, so a kit reads your label, object ToolKit.

02:15 It’s only a label: drilling instanceof Kit stays true.

02:22 Python lists three bases for LoanKit, and its method resolution order runs audited, tagged, then the kit. JavaScript’s extends takes one class.

02:33 So each mixin becomes a factory: an arrow that takes a base class and returns a new class that extends it.

02:44 Tagged wraps the result of super.describe() in tag, and Audited in audit, as the Python methods do. Then you define LoanKit once, by nesting the calls: Kit innermost, Audited outermost.

02:57 Read from the outside in, that’s Python’s order, left to right.

03:03 That one line built this chain. A kit links to LoanKit.prototype, not to the class, so kind was out of a kit’s reach. LoanKit adds nothing, so lookup climbs to the audited layer’s describe.

03:18 Its super call steps to the tagged layer, whose own call steps to Kit.prototype. Then come Object.prototype and null, where the chain ends.

03:27 Python’s method resolution order lists the matching layers, in the same order. Here the analogy stops: Python computes that order from the bases, but this chain is the nesting you wrote.

03:39 Tagged and Audited are functions that build a new class per call, so the layers have no names.

03:47 Now build the two kits with new. The drilling kit’s label is padded with spaces. Spread asks each kit for its iterator, and join adds the commas.

03:57 Before running, check the syntax. node --check prints nothing, so the file parses.

04:04 Then run it. The first line has the label trimmed and the wrappers in order. The drilling kit’s tools appear twice, since each spread gets a fresh iterator.

04:13 All five lines match Python’s.

04:17 So each Python piece has a JavaScript home. Validate in a setter that the constructor runs. Iterate through a symbol method that returns a fresh iterator.

04:27 Compose the mixins once, in Python’s order, read from the outside in.

04:33 Next up is the exercise Finish the Checkout Report. You’ll complete the label printer and the checkout that holds it. Then print the last two lines: the printed label, and a tool count taken through each kit’s own loop.

Become a Member to join the conversation.