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

Sharing Prices and Factories With static

Sharing Prices and Factories With static

Priya Nair’s ten-swim card needs its price: £45 for ten swims. Every card shares that price, so the Python module keeps it on the class, as two class attributes, and cost_per_swim() reads them through self:

Language: Python
class TenSwimCard:
    PRICE = 45
    SWIMS = 10

    def __init__(self, swimmer, swims_left=10):
        self.swimmer = swimmer
        self._swims_left = swims_left

    def cost_per_swim(self):
        return self.PRICE / self.SWIMS

swimmer = Swimmer("Priya Nair")
priya = TenSwimCard(swimmer, 3)
cost = priya.cost_per_swim()
print(f"Ten-swim card: £{cost:.2f} a swim")

Python prints Ten-swim card: £4.50 a swim.

In JavaScript, a value that belongs to the class is a static field: you write static in front of it. From outside, you read it through the class, TenSwimCard.PRICE, as in Python. The method ports the way Python reads the price, with this for self, and .toFixed(2) rounds the cost to two places and gives back text, as :.2f does:

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 Priya’s ten-swim card still needs its price: forty-five pounds for ten swims. In Python, every card shares it, so it lives on the class: two class attributes.

00:12 cost_per_swim() reads them through self, and Python prints four pounds fifty a swim. Now you port it to JavaScript, then a factory for saved cards.

00:24 In JavaScript, a value that belongs to the class is a static field. You write the keyword static in front of each field, the price and the swims.

00:37 The method ports the way Python reads the price: self becomes this. Then toFixed() rounds the cost to two places, as the f-string’s format does, and the template literal builds the line.

00:52 Run the port, and JavaScript logs one line for Priya’s card. What does that line show? Your turn: pause the video and predict.

01:07 NaN pounds a swim. The cost came out as NaN, JavaScript’s number for a result that isn’t a number. And there’s no error: the file ran to the end.

01:21 Priya’s card holds only its own data: her swimmer and her swims left. Asked for the price through self, Python looks on the instance first, doesn’t find it, and looks on its class.

01:33 So Priya’s card gives 45 too. In JavaScript, methods without static are still shared by every card, as in Python: that’s how costPerSwim() runs.

01:46 But static members belong to the class only. The class gives 45, but Priya’s card gives undefined. An instance never looks for static members.

02:00 So the method divides undefined by undefined, and that’s NaN. In Python a missing attribute raises an AttributeError; in JavaScript a missing property gives undefined, so nothing stops, and toFixed() turns NaN into text.

02:17 The fix is to name the class: its price over its swims is 4.5. Now Priya’s card costs four pounds fifty a swim, as in Python. So reach static members through the class name.

02:34 Grace Obi bought her card on the booking site, which saved it as JSON text. In Python, json.loads() turns that text into a dict. Then a class method, from_record(), builds the card.

02:49 It takes the class as cls, and calls it with Grace’s swimmer and her swims. Called on the class, it gives Grace her ten swims.

03:01 In JavaScript, JSON.parse() does what json.loads() does, and gives a plain object, read with dots where Python uses brackets. The class method becomes a static method: the keyword static in front, and no cls parameter.

03:17 It builds the card with new and the class name, from Grace’s record. You call it on the class, as in Python, and it logs Grace with her ten swims.

03:29 Python would also find from_record() through Grace’s card, but in JavaScript, calling it there stops with a TypeError: it isn’t a function.

03:39 Inside a static method, this is the class, as cls is, so new this would work too.

03:47 Watch out for another Python habit: a helper marked @staticmethod usually becomes a plain function in the module, not a static method. And don’t make a method static only because it doesn’t use this yet.

04:00 Static is for what belongs to the class as a whole: a shared price, or a factory that makes cards.

04:09 Python finds class attributes through an instance; JavaScript finds methods without static that way too, but never static members. So mark what belongs to the class as a whole static, and reach it through the class name.

04:24 Next up, Price a Membership Renewal: a teammate’s port should quote Tom Hale’s renewal at three hundred eighty-four pounds.

Become a Member to join the conversation.