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,
}
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;
}
How do we initiate the creation of a new object?
var Nurse1 = new Nurse("Nelly", "ICU", 32);
to access the property we use this syntax:
object.property
//in or case
Nurse1.name
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;
}
}
How to call a function?
object.method()
// in our case
nurse.iv()
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
}
}
Top comments (0)