User Name:


User Email:




This information will only be saved for the purposes of communicating with those who have provided this information voluntarily regarding our services.We will never sell your name or email address to anyone.
© 2018 - First Crazy Developer (Abhishek Kumar)
  

crazydeveloper Implemention of access modifier in JavaScript

Hi guys, when we talk about Object Oriented Programming language, we found some strongly type object avialable  in C#, C++ and Java.

When we talk these language there are lots of access specifier we used to defined scope of method and properties. Today we will discuss that how can we implement these concept in JavaScript, when we talk OOPs in JavaScript?

Look the following code:-

function Employee() {

    var companyName = "FirstCrazyDeveloper"; // Private Variable

    this.firstName = ''; // Public Property

    this.lastName = ''; // Public Property

    this.empSalary = 0; // Public Property

    this.setEmployeeDetails = function(fName, lName, eSal) {

        this.firstName = fName;

        this.lastName = lName;

        this.empSalary = eSal;

    }; // Public Method

    this.getEmpName = function() {

        return 'Mr. ' + this.firstName + ' ' + this.lastName;

    }; // Public Method

    this.getEmpSalary = function() {

        return this.empSalary;

    }; // Public Method

}

If we go through in above code, first we declare  companyName, this is Private variable, which we can not access outside that. Then we declare mutiple properties and methods with this keyword for public access.

//This is an instance method and its call at the only one time when the call is initiated

Employee.prototype.empDetails = function() {
    this.firstName = '';
    this.lastName = '';
    this.empSalary = 0;

}

Now the time comes to declare Static variables in JavaScript

//This is a static vaiable and its shared by all instance.

Employee.empDepartment = "Account";

Now look the final code where u used all above declared properies and variables.


var myEmp = new Employee(); // Create the object of Employee class

When we execute the above code "empDetails" called and initiate all properties. Above instance method called when we create the instance of Employee.

Console.log(myEmp.empDepartment) // Account

Because we  declared a Static variable, so we can access that here with the instance of Employee

myEmp.firstName = 'First Crazy';
myEmp.lastName = 'Developer';
myEmp.empSal = 80000;.

Above all properties defined as public so Employee object direct access that. But we can't access  here  companyName due to declared as private.

Now we can also call public method with this object.

myEmp.getEmpName(); //Mr. First Crazy Developer

myEmp.getEmpSalary(); // 80000

Now we understood how we can use Public, Private and Static in JavaScript.

For more details about Scope and Context level variables please visit Different between Context Vs Scope in JavaScript.


crazydeveloper Home Page 02 July 2015

Become a Fan