OOP Basics of JavaScript

Serra Saracoglu
2 min readAug 9, 2021

Hello hello,

Today we are going to talk about delightful topic.

OOP with JavaScript as we all know it is not totally object oriented programming language but it is designed on a simple object-based model.

And as usual I have homework to do with JavaScript and I will share definitions with you to understand how do we do .

Let’s start with how to create objects,

var objectName={

propertyName:valueOfProperty,

propertyName:valueOfProperty

}

objectName is the name which we want to call our object like(person, student, employee etc.). propertyNames are the names which are the qualifications of our objects and valueOfProperty is the thing what that property has like what is the name age and so on.We use “=” sign as you have seen above .

Here we have one example from real code world;

var Student={

firstName:”Alex”,

lastName:”Sun”,

age:12

}

We have student object above, in JavaScript we can have objects with any classes.They don’t have to belong a class.

After objects we have one another main character of OOP, and here you go everyone we are gonna see Constructor function.

With constructors “this” operator use for to refer current object,

we should have “=” sign for properties and function.

There will be no return.

function student(firstName,lastName,age){

this.firstName=firstName;

this.lastName=lastName;

this.age=age;

}

At above we have our constructor function example.Value of objects depends on the parameters passed on the function.

Properties are the things which are define the objects.You can define properties by assigning them some values.

How do we access properties you think?

We have two way for it, most general one is “dot notation” as you all know and the other is with brackets.

var student{name:”David”}

student.name

student[name]

We can reach properties values as you have seen

We have two more point to explain for that writing.

Functions and classes…

We need function key word then name and prantheses ():)

Code will be executed between {} braces.

function name(parameter1, parameter2, parameter3) {
// code to be executed
}

return keyword will stop function execution.

For classes we are using class keyword and then remember adding constructor() all the time.

class Book {
constructor(name, price) {
this.name = name;
this.price = price;
}
}

In this writing we have remembered basic JS OOP elements.

Next writing will be about applications of them and also encapsulation, inheritance with JS OOP.

How we can implement those to JS?

Stay with good technology, good luck!

--

--

Serra Saracoglu
Serra Saracoglu

No responses yet