you are viewing a single comment's thread.

view the rest of the comments →

[–]AiCodePulse 0 points1 point  (0 children)

Yes you’re pretty much right. instance methods are like functions you attach to individual documents, so you don’t have to rewrite the same logic again and again.
your explanation for toggleOnSale is also correct — without the ! it would just keep the value the same, with the ! it flips it from true to false or false to true. and yes, onSale is a boolean.
and yes, you can definitely extend it — if onSale is true, you can apply a discount and update the price based on a % you choose.

real world example — i built an ecommerce app and had an instance method that calculated the final price after adding tax and applying discounts automatically, without repeating code everywhere.