I can’t really wrap my head around OOP. Like I understand an array and a function and a variable, but I don’t understand what an object is. Why can’t we just use functions and variables everytime we need information or code to run? I code in Python and JS mainly, so objects are truly the most important thing, but I don’t really get it. Also, isn’t every program object orientated? Even C, does it not fit the definitions? If so, how is it any different from C++?
In: Technology
It honestly takes a while to wrap your mind around.
The first thing I’ll note is the notion of an object in JavaScript is so different from any other popular language, I think it’s a bad starting point for how to understand traditional class-based OOP. Like most things involving JavaScript IMO, it’s best to just completely ignore JavaScript’s way of doing something as the “norm”, it will make you more confused as to how it relates to the wider world of programming.
IMO the best way is to do it, is understanding them relative to C structs because that’s where modern classes and objects evolved from in most languages. There is at least one good comment here that identified the connection.
The original idea from Alan Kay was to be able to treat bundles of data like structs as tangible objects. Like, you could define a blueprint for a bird or something in code, called a class. The bird would define its own behaviours or interactions. It would be able to tell the world around it that it’s capable of flying, and how.
You could turn this into a template called a class and create many separate and distinct bird objects that could do all the behaviours of a bird. And maybe you then want to that bird blueprint, and be able to make something more specific, like a duck. So you extend the bird blueprint with duck behaviours. It still has the flying behaviours that most birds have, but then you start adding other behaviours like swimming and floating.
In that respect, you can just take the struct in a languge like C, and start turning them into the idea of being a template for objects you can create called a class. This special struct is just a collection of data that can define its own behaviours and be responsible for its own data. Because it’s responsible for its own data, you can tell it to protect sensitive data. Because it acts like a template, you can use that template to extend it and make other similar templates of data and behaviours.
In his original conception, Alan Kay also had this idea that objects would interact by passing messages to keep them from being tightly coupled. That never quite worked out in reality, because of the way modern OOP evolved from C and I guess Bjorn Stroustrup never got the memo or something.
But yeah, basically OOP is just C structs with extra bonus features.
Now, the above describes class-based OOP.
There was also another interpretation of OOP called prototype-based OOP. I don’t know much about the history or context of that and outside of JavaScript.
Latest Answers