Skip to main content

Object Oriented Programming in JavaScript

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.


  1. Creating Class in JavaScript 
    the following syntax is used for declaring a class in JavaScript:
    1. function Emp() {  
    2.     alert('Emp instantiated');  
    3. }  
    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.
     
  2. Creating Objects of Emp Class
    Using the following syntax we can create an object of the Emp class:
    1. 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
2.Running the code



An alert will be shown on the page load of the web form.

3. Properties of class
We can define the properties of a class in the following way:

  1. function Emp(firstname) {  
  2.     alert('Emp instantiated');  
  3.     this.firstname = firstname;  
  4. }  
  • Passing value to properties
    1. var Emp1 = new Emp("Devesh is Emp of GENPACT INDIA");  
    2. alert(Emp1.firstname); 
  • Snap for defining properties

Sample code

var Emp1 = new Emp("Devesh is Emp of GENPACT INDIA"); 


Advance Javascript 


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.




Now we will learn methods in JavaScript classes.

Use the following procedure to create a sample to help understand how to create methods in JavaScript classes.
  1. Creating Class in JavaScript
    The following syntax is used for declaring a class in JavaScript:
    1. function Emp(NAME) {  
    2.     alert("EMP Instantiated");  
    3.     this.NAME = NAME;  
    4. }  
    Here Emp can be act as a class in JavaScript and this.NAME would act as a property.

    The body of Emp acts as the constructor and is called as soon as we create an object of the class


Calling functions
  1. var Emp1 = new Emp('DEVESH');  
  2. var Emp2 = new Emp('ROLI');  
  3.   
  4. Emp1.Fun1();  
  5. Emp2.Fun1()  
  6. }  
Here we created two instances of EMP, EMP1 and EMP2 and passed a parameter to the constructor to assign a NAME Property.

Using EMP1.Fun1() and EMP2.Fun1() we are calling the functions.

We have defined an alert inside this function:


Running the code
After running the code we will get two alerts, each for EMP1.fun1() and Emp2.Fun1();



  1. Other ways to create JavaScript objects

    We can create an object in JavaScript in the following way also:
    1. var EMP = {  
    2.     firstName:"ROLI",  
    3.     lastName:"GUPTA",  
    4.     age:28,  
    5.     sex:"F"  
    6. };  
  2. Accessing EMP objects



    1. alert("EMP NAME: " + EMP.firstName + " " + EMP.lastName + " EMP AGE: " + EMP.age + " EMP SEX : " + EMP.sex);
    Complete Code
    1. var EMP = {  
    2.    firstName: "ROLI",  
    3.    lastName: "GUPTA",  
    4.    age: 28,  
    5.    sex: "F"  
    6. };  
    7. alert("EMP NAME: " + EMP.firstName + " " + EMP.lastName + " EMP AGE: " + EMP.age + " EMP SEX : " + EMP.sex); 
  1. Running the code
Defining Methods
  1. var EMP = {  
  2. firstName: "ROLI",  
  3. lastName: "GUPTA",  
  4. age: 28,  
  5. sex: "F",  
  6. fullName: function () { return this.firstName + " " + this.lastName }  
  7. };  
  8. alert("Full name is: "+EMP.fullName());  
We have defined the fullName method inside the EMP class.

It returns the join of fname and lname.

Running the code



Comments

Contact Form

Name

Email *

Message *

Popular posts from this blog

Exporting DataTable to Excel in C# Using Interop

Introduction I would like to share a utility that can be used to export a datatable to Excel file using C#. We have various approaches to perform this activity. In ASP.NET we can do this by changing the Content type to xls but in C# .Net we might have various other approaches. Through this article we will learn how to export a data table to Excel using Interop. Code and Steps Here in this application we will use a Sample datatable and then learn how to export data to an Excel file. We will learn the following things in this article: Creating Excel file using C# Writing data to cells Formatting data to cells Working with Excel range 1. Adding References First we need to add a reference for Microsoft.office.interop.Excel as in the following: 2. Adding sample data table to the code Use the following code to add the sample data table:     static   DataTable  GetTable()     {       ...

Binding CheckBoxList in ASP.Net MVC

Blogspot Introduction I would like to share how to bind the checkbox list in MVC. I will use a Model (a class file) to define various attributes for checkboxes. For a basic understanding of MVC kindly use the following link: ASP.NET MVC Overview The following is the procedure. 1. Creating Model We created a "SubjectModel" class under the Models folder and defined the following two properties: Subject: to display text Selected: to display check/uncheck 2. Creating the controller We created a Controller "BindingCheckBoxController" under the controllers folder We created an Action having the name = "DisplaycheckBoxes" We created a list of Subjects (Subject model class) Returning a list of subjects to the View. public  ActionResult DisplayCheckboxes() {     List<SubjectModel> listsubject =  new  List<SubjectModel>();     listsubject.Add( new  SubjectModel( "Physics" , true )); ...

Indexes In SQL SERVER in Hindi Part 1