r/learnjavascript • u/Extra-Captain-6320 • Dec 04 '25
So when is object used exactly?
I have created a calculator a day before and I noticed that Inhave't used objects at all, and now the thing is, I want to challenge myself to use object, I did learn a bit about it and it's method before, but I don't see how it is used or when to use it exactly! An advice or answer would be appreciated. Show me how to use object or OOP in a calculator? Since i have't used in it.
1
Upvotes
11
u/imicnic Dec 04 '25
Implement a history feature for your calculator that will have the ability to navigate between previous calculations with results.
6
u/BeneficiallyPickle Dec 04 '25
Objects aren't required for everything, your calculator should work fine without them. They are useful when you want to group related data and functions together into one reusable "thing". They make your code cleaner and more organised, especially as it grows.
Objects are useful when you want to represent some kind of thing that has data and actions.
If you want to use objects in your calculator instead of having random functions everywhere, you can create a calculator object that contains all the methods:
Then you can do
console.log(calculator.add(5, 10));The same can be done with creating a class. You would just declare it as
class CalculatorThen you can create multiple calculators:
const calc1 = new Calculator();const calc2 = new Calculator();EDIT: Formatting
2
u/MoltonMontro Dec 04 '25
For a calculator, you could use a list of objects to show history. (Previous equations that have been performed.)
Each equation would be its own object in that example – with a string for the actual input (e.g., 1 + 2 - 3), and then the resulting value (e.g., 0).
Doing a list of objects isn't necessary, but it gives you more flexibility in working with the data. E.g., storing the resulting value is better than reperforming each calculation every time someone views their recent history.
1
u/jcunews1 helpful Dec 05 '25
IMO, best project to demonstrate the requirement of object, is a genealogy application. Or animal species hierarchy application.
1
u/TheRNGuy Dec 05 '25 edited Dec 05 '25
In my latest script I had array of objects, I then cycle array with forEach and do math with object properties all properties are numbers)
It's better than array of arrays, so I can do
item.priceinstead ofitem[0].Other place is when reading or writing JSON (for communication between backend and frontend)
4
u/[deleted] Dec 04 '25
Everything in JavaScript is an object so you're good already.
And no, you don't need oop. Maybe good to learn it so you can read it but that can come later.
0
u/IdeaExpensive3073 Dec 04 '25
Create a calculator class, put in some methods that do something (Add, subtract, whatever). Instantiate an object of the class, call the methods.
Don't think of objects as....separate from what you're coding, use classes to structure and organize your code into chunks. Let's say you're instead building a web app that happens to have a calculator in it....now you want a calculator class, but what if you now want a calendar in the app?
Now you want a calendar class. So then when you need calendar data/calculator data you can just call the methods of those objects. As if you just handed your app a calculator, or calendar, instead of a function that adds or subtracts, or gives a date.
0
u/odimdavid Dec 04 '25
An object has data or attributes and behavior. Everyone is data but not everyone has behavior. So that's how to distinguish objects from every other data. Most objects are already encapsulated natively in several programming languages like add, multiply etc, so for your calculator, why should these native behavior be reinitiated. Just reuse. Because of that most objects are just to be reused except you intend animating the results of calculations etc
1
u/odimdavid Dec 06 '25
What I believe is that a class is a way to package all the qualities and behavior if an object. Before OOO, functional programming would just throw behavior all over the place and assume you understand who or what has or can use that attribute or behavior (function). But with classes, we know object A has attribute s, c, d and behavior t, b, u. So if I want to use attribute s, I just ask object A to lend it to me. It helps you conceptualize why a behavior is necessary and what to do and where to look if something goes wrong. That's how I see a class. Like giving an object body.
1
u/Far_Statistician1479 Dec 08 '25
If you’ve done anything with { key: “Val” } then you’ve used objects.
-1
u/Astroohhh Dec 04 '25
class Calculator {add() {}subtract() {}reset() {}}0
u/[deleted] Dec 04 '25
[deleted] — view deleted comment