DEV Community

Lilajy
Lilajy

Posted on

POO or OOP but in French.

Object Oriented Programming is a way of coding. I came across this notion starting to code with NodeJS, an object-oriented language.

This is the way of thinking:

An object can have properties and methods. The properties are the “Be” and the methods are the “Do”.

Take a nurse as an example.

Properties

You create an object Nurse that has several properties.

var Nurse ={
name: "Lila",
specialty: "Biological analysis",
Age: 30,
}
Enter fullscreen mode Exit fullscreen mode

This is called an Object Literal. You use it when you only need one object of this type.

Constructor

We use a constructor function to create more objects with the same properties. It is like a new “version” of the object.

⚠️ To declare a constructor function, we must give it a name starting with a capital.

To create the new object, we give the constructor function properties as inputs.

Inside the function, we match the inputs with the properties’ names.


function Object(proprety1, proprety2, proprety3){
this.proprety1 = proprety1;
this.proprety2 = proprety2:
this.proprety3 = proprety3;
this.methode = function(){
    //function
    }
}

// in our case

function Nurse(name, specialty, age){
this.name = name;
this.specialty = specialty:
this.age = age;
}

Enter fullscreen mode Exit fullscreen mode

How do we initiate the creation of a new object?

var Nurse1 = new Nurse("Nelly", "ICU", 32);
Enter fullscreen mode Exit fullscreen mode

to access the property we use this syntax:

object.property
//in or case
Nurse1.name
Enter fullscreen mode Exit fullscreen mode

Method

If we want our object to do something, we have to attach it to a function. It’s called a method.

It says my object can do this: method.

var nurse ={
name: value,
specialty: value,
age: value,
iv: function(){
    put an iv;
    }
}
Enter fullscreen mode Exit fullscreen mode

How to call a function?

object.method()
// in our case
nurse.iv()
Enter fullscreen mode Exit fullscreen mode

How to add a function in a constructor function:

function Object(proprety1, proprety2, proprety3){
this.proprety1 = proprety1;
this.proprety2 = proprety2:
this.proprety3 = proprety3;
this.methode = function(){
    //function
    }
}
function Nurse(name, specialty, age){
this.name = name;
this.specialty = specialty:
this.age = age;
this.iv = function(){
    put an iv
    }
}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)