Introduction
Javascript is Prototype-based programming and a style of object-oriented programming in which classes are not present. It can support oops because it support inheritance through prototyping as well as properties and methods Object oriented programming in JS know as Prototype-based programming.
a. Creating Class in javascript
Following syntax is used for declaring class in JS
function Emp() {
alert('Emp instantiated');
}
Here Emp can be act as Class in JS
Body of Emp act as constructor as called as soon we create object of class.
- Creating Objects of Emp Class
Using following syntax we can creating object of Emp class
var Emp1 = new Emp();
As soon we create object of Emp class it constructor we be called
Here above function Emp would be treated as class in JS
c. Running the code
Alert will be shown on page load of web form.
d. Properties of class
We can define properties of class in following way
function Emp(firstname) {
alert('Emp instantiated');
this.firstname = firstname;
}
i) Passing value to properties
var Emp1 = new Emp("Devesh is Emp of GENPACT INDIA");
alert(Emp1.firstname);
ii) Snap for defining properties
iii) Complete code
iv) Running code
When we run the code we got alert of the string which we are passing to class of Emp.
var Emp1 = new Emp("Devesh is Emp of GENPACT INDIA");
Im thankful for the article post.Thanks Again. Fantastic.
ReplyDeletepython training
angular js training