Object Oriented Programming
Object Oriented Programming
Programming
(or not, that is the question)
November 2022
If you are interested about If you would like to know what OOP
OOP in JavaScript, choose was intended to, choose Water and
Wind and meet with the see the
magnificent Divine Light
Diva
START LECTURE
Introduction
data (state)
behavior
The subtle detail there about that method is not being pure means a lot when unit tests are considered.
And unit tests are always considered. Theoretically, however, it is possible to write always green and
therefore perfectly pointless unit tests, but even Zorg is not that evil – you should not be, either.
1. Encapsulation
2. Inheritance
3. Polymorphism OOP development is always SOLID
no kidding, you
should read this
If you open any book (the good ones, with the owl
examples), the chance that you’ll run into these terms, is
100%. So, let’s take aside these first*!
* from this start, as you may already guess, these are not that important for us in JavaScript – still, you will be asked! ¯\_(ツ)_/¯
Encapsulation is a concept
an object “class”:
OOP is not about the data + behavior
implementation details. Here, we
implemented proper encapsulation with data is private and
simply a closure*. decoupled
owl inherit
everything what
Animal has
Postal
Animal Animal Animal Service
Postal
Owl Bird Bird Worker
Owl Owl
Remember: as peace, inheritance is a concept merely. Whether it is built-in and in what form to the
language is another question. While multiple inheritance is not part of the JavaScript, it can be
implemented in many different ways. Should be?
Let’s ask this way: should you rely on inheritance at all? Or maybe composition would be better?
Polymorphism, however, is not a problem, if the code does not use inheritance.
* like SOLID, composition over inheritance, even the design patterns were introduced to provide workarounds for OOP issues
* “I like to say that JavaScript is Smalltalk’s revenge on the world’s misunderstanding of OOP.”
almost infinite.
For example:
• in Global context
• in Function context
• in Function context – strict mode
• in a constructor
• in a constructor, called without new
• in a constructor, called without new – strict mode
• in a method call
• in a function call (a method assigned to a variable)
• in Arrow functions
• …
in Global context
(equals to the global object)
in Function context
(equals to the global object)
oops!
Object
a function call =