Object-Oriented Programming Flashcards
What is OOP?
A STYLE of programming centered around objects rather than functions.
What OOP are there?
C#, Java, Ruby, Python, JavaScript (controversial)
What relevance does OOP have to frameworks?
A lot of frameworks were designed with OOP in mind. E.g. Angular
What is an object?
An object is a combination of related variables (properties) and functions(methods) grouped together into a unit.
It is an instance of a class that has attributes, behaviour and identity and is defined by the class definition. E.g. class named Vehicle and objects could be Van, Truck, Car
What are the 4 pillars of OOP?
Encapsulation, Abstraction, Inheritance & Polymorphism
What is encapsulation?
Encapsulation is the grouping together of the related variables and functions.
When the functions exist inside the object, we don’t need to pass it any parameters, because the parameters are all modelled as properties of the object.
Simply, this means they exist within the object itself, therefore do not need to be passed into the function.
E.g.
let employee = {
baseSalary: 30_000,
overtime: 10,
rate: 20,
getWage: function( ) { return this.baseSalary + (this.overtime * this.rate); } };
What is the benefit of encapsulating the functions and variables?
Reducing complexity of your programme & increasing the reusability of your objects.
Fewer parameters need to be passed. This means that your code will be more easily maintainable.
“The best functions are those with no parameters!” - Uncle bob
What is abstraction?
Hiding the complex details (implementation) & expose only the essentials )
We do this by isolating the properties and methods.
- ) simpler object interface (we can use and understand the object with just a few properties and methods)
- ) reduces the impact of change - changing things inside the object will not leak effects outside, because no code outside of the object touch those methods
E.g a DVD player
- we press play and don’t care what happens on the inside
- the complexity is hidden from us, inside the DVD player, this is abstraction in practice
What is inheritance?
Instead of redefining common properties and methods for each object, they can be defined once in a generic object and then inherited by other objects.
It allows us to re-use code easily & eliminate redundant code.
Reduces opportunity for bugs.
Makes it easier to change/maintain code as you just need to look in one place
What is polymorphism?
A technique that allows you to get rid of long if/else & switch statements. This is basically refactoring.
Poly means many
Morph means form
= many forms
What are functions in OOP?
Functions in OOP are objects!
The purple cubes in VSC are methods.
The blue rectangles in VSC are properties.
How can we add properties to an object?
There are two ways we can add properties:
1.)We can generate a new property via the dot notation.
E.G. myObject.newProperty = _____;
2.)Using bracket notation
E.G. myObject [ ‘newProperty’ ] = ____;
How can we access properties?
Either with . or [ ] notation.
dot . notation is best, but bracket notation can be useful for property names that are not valid identifiers such as special characters
E.G. myObject[ ‘object-property’ ]
How can we delete properties?
By using the keyword ‘delete’
E.G. delete myObject.myProperty
How can we iterate over an object?
1.) FOR…IN loop. This loops over the object regardless of length and we can access both the key & value
for ( let key in circle ) {
console.log( key, circle[key])
}
the above would output the key:value pair
2.) using Object.keys
const keys = Object.keys(circle) console.log(keys);
the above would output all keys into an array WITHOUT the values
3.) using the IN operator to iterate over the properties
if ( ‘radius’ in circle )
console.log( ‘Circle has a radius.’ );
= if the circle object has a property of radius console log the message