JS interview in 2 minutes / Object-Oriented Programming (OOP)

Question:
What is object-oriented programming (OOP)?

Quick answer:
It is an agreement describing how you can write programs, grouping state, and related operations in one place.

There are classes – a boilerplate for objects, objects – actual contain…


This content originally appeared on DEV Community and was authored by Nikita Kozlov

Question:
What is object-oriented programming (OOP)?

Quick answer:
It is an agreement describing how you can write programs, grouping state, and related operations in one place.

There are classes - a boilerplate for objects, objects - actual containers for the data, methods - operators over data in these objects.

// Btw, it may be useful to know the other paradigms, more info on the wiki.

Longer answer:
Let's start from some simple problem which we will try to solve using OOP.

Imagine we are building new Facebook, but for the dogs. Awesome startup idea!

Ok, so we are dealing with dog profiles, what data is there?

{
  name: 'Doggert',
  age: 2,
  isGood: true,
},
...

We need some way to create profiles like this in a blink of an eye and do some common things like barking.

At this point, OOP kicks in. Let's create a boilerplate code that will help us easily create objects like the previous one.

class DogProfile {
  constructor(name, age) {
    this.name = name
    this.age = age
    this.isGood = true
  }
}

const doggert = new DogProfile('Doggert', 2)

Now we need to figure out how to bark, as it is required for every mannered dog.

class DogProfile {
  // ...
  bark() {
    alert('Bark!')
  }

  barkInEnglish() {
    alert(`Hello my friend! My name is ${this.name}.`)
  }

  changeName(name) {
    this.name = name
  }

  old() {
    this.age++;
  }
}

// ...
doggert.barkInEnglish()
doggert.changeName('Doggert the Great')
doggert.barkInEnglish()

Finally, we have a class, which helps us create new data, objects which store data, and methods that help us to work with the data.

Real-life applications:

There are not only ? and ?. In real-life applications, you should consider few caveats using this approach.

For example, you should consider how do you extend or refactor existing classes. Imagine you need to add CatProfile, it is the same as DogProfile, but still different. How do you handle situations like this?

At another moment you need to add admin profiles and admin permissions. How do you handle it? Will you need to update all classes?

There is another funny sound issue banana, monkey, jungle problem. It is when you need to create a banana, but you need a monkey to hold it, but the monkey only lives in a forest.

So there are a lot of possible issues which you need to be aware of in advance. With great power comes great responsibility as you may have heard ?

p.s.: I'm not trying to be 100% accurate on every definition, but just trying to describe it in simple words. Sorry in advance ?

Resources:
wiki/OOP
wiki/programming_paradigm

Other posts:

Btw, I will post more fun stuff here and on Twitter. Let's be friends ?


This content originally appeared on DEV Community and was authored by Nikita Kozlov


Print Share Comment Cite Upload Translate Updates
APA

Nikita Kozlov | Sciencx (2021-04-28T20:06:13+00:00) JS interview in 2 minutes / Object-Oriented Programming (OOP). Retrieved from https://www.scien.cx/2021/04/28/js-interview-in-2-minutes-object-oriented-programming-oop/

MLA
" » JS interview in 2 minutes / Object-Oriented Programming (OOP)." Nikita Kozlov | Sciencx - Wednesday April 28, 2021, https://www.scien.cx/2021/04/28/js-interview-in-2-minutes-object-oriented-programming-oop/
HARVARD
Nikita Kozlov | Sciencx Wednesday April 28, 2021 » JS interview in 2 minutes / Object-Oriented Programming (OOP)., viewed ,<https://www.scien.cx/2021/04/28/js-interview-in-2-minutes-object-oriented-programming-oop/>
VANCOUVER
Nikita Kozlov | Sciencx - » JS interview in 2 minutes / Object-Oriented Programming (OOP). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/28/js-interview-in-2-minutes-object-oriented-programming-oop/
CHICAGO
" » JS interview in 2 minutes / Object-Oriented Programming (OOP)." Nikita Kozlov | Sciencx - Accessed . https://www.scien.cx/2021/04/28/js-interview-in-2-minutes-object-oriented-programming-oop/
IEEE
" » JS interview in 2 minutes / Object-Oriented Programming (OOP)." Nikita Kozlov | Sciencx [Online]. Available: https://www.scien.cx/2021/04/28/js-interview-in-2-minutes-object-oriented-programming-oop/. [Accessed: ]
rf:citation
» JS interview in 2 minutes / Object-Oriented Programming (OOP) | Nikita Kozlov | Sciencx | https://www.scien.cx/2021/04/28/js-interview-in-2-minutes-object-oriented-programming-oop/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.