Object oriented programming
object oriented programming (OOP) is a programming pardigm where data is dealt with in a similar fashion to objects in a real world. A simple example would be cars. Each car has similar qualities like color, model, make, these are called properties. Each car also functions in a similar way and can do things like start, brake, accelerate, steer, etc. these are know as methods but can also be referred to as functions.
Using the information above here is a simple example of what a car object would look like in javascript. Creating an object using this method is called writing an object literal
var civic = {
make: 'Honda',
model: 'Civic',
color: 'blue',
gallonsOfGas: 5,
speed: 0,
accelerate: function(){
this.speed += 1;
this.gallonsOfGas -= 0.01;
},
brake: function(){
this.speed -= -1;
}
}
Another way of creating an object is using the new keyword to first create or instantiate the object, then adding values using dot notation or passing the key like so civic['model'] = 'Civic';
var civic = new Object();
civic.color = 'red';
civic['model'] = 'Civic'; // alternate syntax
Writing this information out can be tedious, what if we had a quick way to enter all of this information, and let the computer take care of constructing the object? Fortunately we do! This is what's known as a constructor function.
function Car(make, model, color, gallonsOfGas){
this.make = make;
this.model = model;
this.color = color;
this.gallonsOfGas = gallonsOfGas;
this.speed = 0;
this.accelerate = function(){
this.speed += 1;
this.gallonsOfGas -= 0.01;
},
this.brake = function(){
this.speed -= -1;
}
}
Then we can easily create a new Car using the following.
var corvette = new Car('Chevrolet', 'Corvette', 'red', '0');
Finally, we access these properties using dot notation, and we access the methods the same way, but to use the method we need to call them by adding () to the end.
corvette.speed; // returns 0
corvette.accelerate(); // returns undefined, but adds 1 to speed
corvette.speed // now returns 1
Posted Oct 23, 2017, 4:22 AM UTC · edited
Using a purely functional language, like Haskell, I can also model this structure using a data type with nested fields and writing functions for working with it. Including a convenient constructor.
Is my understanding correct?
Also, as far as I know, Smalltalk is the first OOP language and it had no notion of a class or method, instead having objects and something called "message passing". If even the notion of a method is not necessary for a language to be OOP, what differences from FP remain, apart from mutability? Or is mutability the only factor that fundamentally distinguishes OOP paradigm from FP?
I know that paradigms can be combined in the same codebase, but as I learn more OOP and FP, I start to struggle to really differentiate them. I would appreciate an explanation.
Upvote for you, friend. I would love to see an ELI5 about functional programming in practice, not as an esoteric concept.
[deleted]
This is interesting. But then I still don't understand what OOP is.
The only remaining thing is that the data and behavior are represented as parts of a single entity, an "object" (often, but not necessarily, an instance of some class/type).
Is that it? Fundamentally, is combining data+logic the only unique part of OOP as a paradigm?
[deleted]