DEV Community

Introduction to OOP in Javascript

Ndifreke Friday on November 17, 2017

What is this OOP thingy. "Object-oriented programming (OOP) is a programming language model organized around objects rather than "actions" and dat...
Collapse
 
st2rseeker profile image
Edward Goikhman

I would suggest changing the inheritance example to a proper is-a relationship to illustrate the point - I assume many OOP-newbies will find this article, so better not confuse them :)

There is no sense in Person extending Organization (essentially being one).

Collapse
 
ndiecodes profile image
Ndifreke Friday

On it......tanx for the response

Collapse
 
copy_pasta_chef profile image
Essien

Thanks for the write-up Nddy.

In your third example, was the reason you chose to call description() in bio() just to show us that you can call class methods in other methods of the same class or was there another reason?

Also, I think you have a typo here: "I told you they were not scary, getters get's property value and setters changes our changes property value."

Thanks.

Collapse
 
ndiecodes profile image
Ndifreke Friday

no other reason

thanks for noticing d typo.

Collapse
 
linaran profile image
Deni • Edited

Bad example on inheritance. Inheritance describes an IS-A relationship. Obviously a person is not an organization and such an inheritance doesn't make sense. Also "this" refers to the instance of the class not the class itself. Second description implies that changes on this would influence all instances of some class which is not the case.

Collapse
 
ndiecodes profile image
Ndifreke Friday

you are absolutely right...
why I do agree I did use a bad example while trying to stick to the storyline,
I tried to keep it as simple as possible as I did not expect a newbie to understand instances

Collapse
 
linaran profile image
Deni

Keeping instructions is nice but they need to be precise otherwise a newbie could conclude that "this" is some kind of a static variable and that could make learning difficult down the road.

Just edit the "this" part, overall the article is fine.

Collapse
 
ndiecodes profile image
Ndifreke Friday

thanks for contributing.

Collapse
 
ndiecodes profile image
Ndifreke Friday • Edited

It sure is a bad example none-the-less, I am working on changing it and still keeping it as simple as possible

Collapse
 
imsop profile image
Rowan Collins

Wait, now every Person is a Father, but not every Father is a Person? I think maybe you should spend more time understanding inheritance yourself before trying to explain it to others.

Collapse
 
ndiecodes profile image
Ndifreke Friday

A person is a Father
A Father is a person

In this case it goes both ways

I am not perfect... I get that!

you see something that doesn't feel right you let me know.

Collapse
 
imsop profile image
Rowan Collins

Not every Person is a Father, and that's what inheritance represents. If class B inherits from class A, it means that every instance of class B has the abilities of class A, and anywhere that expects an A can be given a B instead.

This is probably easier to understand if we add a third class to the example: Mother. Intuitively, a Mother is a Person, but not a Father; and a Father is not a Mother either. So code that expects a Father and is given a Mother won't work; but code that expects a Person can be given a Father or a Mother. So, we would write "class Father extends Person", and "class Mother extends Person".

Thread Thread
 
ndiecodes profile image
Ndifreke Friday

Thanks man... I figured that out from your first response

I think this article has helped me more than it will help any reader and I made some changes which you can check out too

I'm grateful.

Collapse
 
lancecontreras profile image
Lance Contreras

I like the way you wrote it, you made it look really easy.

Collapse
 
ndiecodes profile image
Ndifreke Friday

thanks Lance, I'm glad you liked it

Collapse
 
vicemond profile image
Vic Emond

You have an extra "class Person {" in there...

Collapse
 
ndiecodes profile image
Ndifreke Friday

can you be more specific pls

Collapse
 
ndiecodes profile image
Ndifreke Friday

Seen it... tankx

Collapse
 
aminspeaks profile image
Amin Mohamed Ajani

Classes in JS is less syntactical sugar and more syntactical salt.

Collapse
 
ndiecodes profile image
Ndifreke Friday

lmao

Collapse
 
harveylewis profile image
Harvey Lewis

In the first example, I believe the code at the bottom should be
'Console.log(citizen.name)'
Rather than
'Console.log(a.name)'
no?

Collapse
 
ndiecodes profile image
Ndifreke Friday

Yes you are right... I kinda changed to citize at a late hour.... forgot that one

Editing it now... tanx