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

Computing a Property on Demand

Computing a Property on Demand

In this lesson, you give a plain JavaScript object a property that’s worked out each time you read it. Harbour Prints keeps a proof order’s quantities in a list. In Python, the Order class has a total property, and reading order.total with no parentheses runs the method and prints 5.

The JavaScript export builds the order as a plain object with no class, and a stored total would go stale as soon as a quantity changes. An object literal takes a getter as a class does, with the get keyword before the method name, so get total() plays the part of Python’s @property, though Python needs a class for its property.

Below it, set extra is a separate property, with only a setter and no getter at all. Assigning to order.extra would call it with a late quantity, which it pushes onto the list. In Python, a setter extends a property you’ve already defined, under the same name. Nothing assigns to extra yet.

The last lines log the total and the descriptor of one key. Every property has a descriptor, the record of its value and its flags. In JavaScript, the word means this per-property record, not Python’s descriptor protocol:

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 give a plain JavaScript object a property that’s worked out each time you read it. Harbour Prints keeps a proof order’s quantities in a list.

00:10 In Python, the Order class has a total property that adds them up. You read order.total with no parentheses, and Python runs the method.

00:19 It prints 5. The JavaScript export builds the order as a plain object, with no class, and a stored total would go stale when a quantity changes.

00:30 In JavaScript, a const names the order, and an object literal holds the quantities array. The literal takes a getter as a class does, with get before the method name, so get total() plays the part of @property, though Python needs a class for it.

00:47 You’ll read its body when it runs. Below it, set extra is a separate property, with a setter and no getter. Assigning to order.extra would call it, and it pushes a late quantity onto the list.

01:00 In Python, a setter extends a property of the same name. Nothing assigns to extra yet.

01:07 A new last line logs order.total with console.log(), the way Python’s print() does. Run the file with node, and it prints 5.

01:17 The read has no parentheses: reading total calls the getter. Inside it, this is the object you read from, like self, but never listed as a parameter.

01:29 JavaScript has no built-in like Python’s sum(), so the getter counts by hand. let declares a count you can reassign. The loop runs one statement, usually braced, and walks the quantities, adding each one with +=, as in Python.

01:47 Every property has a descriptor, the record of its value and its flags. In JavaScript, the word means this record, not Python’s descriptor protocol. Two new lines ask Object.getOwnPropertyDescriptor() for the key quantities and log it.

02:03 It’s a data descriptor. Its value is the array, and its three flags, writable, enumerable and configurable, are all true, as for any property written in a literal.

02:16 Now change the key to total and run it again. So which of the four fields you saw for quantities will total keep? Your turn: pause the video and predict.

02:32 Only the two flags. Value and writable are gone.

02:37 In their place, get holds the getter function. And set is undefined, JavaScript’s value for “nothing here”, because total has no setter.

02:48 total is an accessor property, and this is its accessor descriptor. The order stores no 5: each read calls get again.

02:58 Change the key to extra, and the setter’s descriptor mirrors it. get is undefined, so a read of order.extra gives undefined, and set holds the setter.

03:09 Everyday code would use a method to add a quantity. Both keep the two flags. But a descriptor never mixes value and writable with get and set: Object.defineProperty() throws a TypeError if you try.

03:24 Back in Python, a new line prints vars(order), what the instance itself holds. There’s only the quantities. The property is an object on the Order class, and its fget and fset hold the same pair of functions as get and set do.

03:41 That’s where the analogy stops. A getter written in an object literal sits on the object itself, as its own property. A class getter in JavaScript lives on the prototype, as Python’s property lives on the class.

03:57 When a property should follow other data, give the object a getter. An accessor property’s descriptor holds get and set where a data property’s holds a value.

04:08 Nothing has changed the quantities yet, so you’ve only seen 5. Next up is the text lesson Preserving a Getter or Its Value. There, you’ll copy the order two ways, then add a quantity: one copy keeps its old total, and the other keeps the getter.

Become a Member to join the conversation.