Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Object Oriented Programming in JavaScript: Part 1
WhatsApp
Devesh Omar
4y
19.7k
0
1
100
Article
Introduction
JavaScript is a prototype-based programming style of object-oriented programming in which classes are not present.
It can support OOP because it supports inheritance through prototyping as well as properties and methods.
Object-Oriented Programming in JavaScript is known as prototype-based programming.
Creating Class in JavaScript
the following syntax is used for declaring a class in JavaScript:
function
Emp() {
alert(
'Emp instantiated'
);
}
Here Emp can act as a class in JavaScript.
The body of Emp acts as a constructor and is called as soon as we create an object of the class.
Creating Objects of Emp Class
Using the following syntax we can create an object of the Emp class:
var
Emp1 =
new
Emp();
As soon as we create an object of the Emp class the constructor will be called.
Here the above function Emp would be treated as a class in JavaScript.
Running the code
An alert will be shown on the page-load of the web form.
Properties of class
We can define the properties of a class in the following way:
function
Emp(firstname) {
alert(
'Emp instantiated'
);
this
.firstname = firstname;
}
Passing value to properties
var
Emp1 =
new
Emp(
"Devesh is Emp of GENPACT INDIA"
);
alert(Emp1.firstname);
Snap for defining properties
Complete code
Running code
When we run the code we get an alert of the string that we are passing to the Emp class.
var
Emp1 =
new
Emp(
"Devesh is Emp of GENPACT INDIA"
);
Conclusion
This document contains the basics of OOP in JavaScript.
Next article:
Object-Oriented Programming in JavaScript -Part 2
Class in javascript
JavaScript
JavaScript Objects of Class
JavaScript Properties of class
Up Next
Ebook Download
View all
Frontend Developer Interview Questions and Answers
Read by 955 people
Download Now!
Learn
View all
Membership not found