Object Oriented JavaScript - Part I

What is JavaScript?


JavaScript is a prototype based scripting language with dynamic typing and has first-class functions. Previously it is mostly used in web and mobile browser application development. These days it is used for performing server side operation. The best example is Node JS.

What is static and dynamic languages?


In static languages, the process of verifying & enforcing constraints of type and identifying the behaviour of the code can happens at compile time itself. Some of the static languages are Java, C#, etc.,

In dynamic languages the above process is can happens only at run time. This feature will helps to include new code, modifying type systems, and extending objects at run time.

What is first-class functions?


In many languages like Java and C# have some options for defining class. For an example, this is my Person class in Java.
class Person {
  String firstName;
  String lastName;
}
In JavaScript we don't have any keyword like "class" for defining classes. In JavaScript all functions are considered as a class. So we can define a class by using function in JavaScript and this is called as first-class functions. The equivalent Person class in JavaScript is written by something like this.
function Person() {
  this.firstName;
  this.lastName;
}
Object oriented programming has lot of features like Encapsulation, Polymorphism, and Inheritance. It is possible in JavaScript for achieving all the OOP features and for becoming a better JavaScript programmer, we should know that.

Public, Privileged and Private members in JavaScript.


The public members are always visible to public means any functions can read, modify and delete members, or add new members of the object. There are two ways for putting members in a new object.

By using constructor - This way commonly used for initializing public member variables. For an example,

function Person(fname, lname) {
  this.firstName = fname;
  this.lastName = lname;
}
By using prototype - This way commonly used for adding public member to an object. For an example,
Person.prototype.getFirstName = function() {
  return this.firstName;
}
Person.prototype.getLastName = function() {
  return this.lastName;
}
Person.prototype.setFirstName = function(fname) {
  this.firstName = fname;
}
Person.prototype.setLastName = function(lname) {
  this.lastName = lname;
}

Lets see how to create an object  and invoke member function.

var person1 = new Person('Suriya', 'A');
person1.getFirstName(); //Result = Suriya
person1.setFirstName('Gnana');
person1.getFirstName(); //Result = Gnana

We have created public members in javaScript. Lets discuss a way for creating private members in JavaScript and this is a way for encapsulating data from outside.

function Person(fname, lname) {
 var firstName = fname; //Private variable 1

 var lastName = lname; //Private variable 2

        var fullName = function() { //Private function 1
  return firstName + ', ' + lastName;
 }

 this.setFirstName = function(fname) { //Privileged method 1
  firstName = fname;
 }

 this.setLastName = function(lname) { //Privileged method 2
  lastName = lname;
 }

 this.getFirstName = function() {//Privileged method 3 
  return firstName;
 }

 this.getLastName = function() { //Privileged method 4 
  return lastName;
 }

 this.getFullName = function() { //Privileged method 5
  return fullName();
 }
}

Privileged methods can have access for private variables and methods. It is possible to delete or replace a privileged method but we can't alter this method or we can't force this method for displaying internal information of an object. Lets test the Person class by creating new object and invoking its methods.

var person1 = new Person('Suriya', 'A');
person1.getFullName(); // Result - Suriya , A

The private members are defined by without using "this" and they are declared like a normal variable declaration in JavaScript. Here the variables firstName and lastName are private variables and that are not accessible from outside of an object. For an example, we can't access the firstName and lastName property directly by executing below code.

person1.firstName; // Result - undefined

We know JavaScript is a dynamic language that allows to modify the object at run time. What I am going to do is, after creating an object for a Person class, going to override some of the privileged member functions of an object and trying to get access of the private variables. The pure object oriented programming won't allow to access private variables, but it is a time to test JavaScript.

var person1 = new Person('Suriya', 'A');
person1.getFullName(); // Result - Suriya , A
person1.setFirstName = function(fname) { //
  firstName = fname;
}
person1.setFirstName('Gnana');
person1.getFirstName() //Result should be 'Suriya' not 'Gnana'. 

Check the below screen.


So in JavaScript we can't access private members from outside world without getting help from privileged or public methods.

Now we have created Public, Private and Privileged variables and methods in JavaScript. In my next part will explain about Inheritance and other object oriented implementations in JavaScript.

Comments