Programming done using data and methods as bundle of objects is called Object Oriented Programming. Example: Java, C…. Prototype based programming is an object oriented programming where objects are present instead of classes. Objects are used as prototype to define behavior and functionalities. 

The following OOP's concepts are applied on objects in Javascript:

Namespace

Grouping identifiers, methods and functions under application specific name is called namespace. In Javascript all the declarations and definitions are stored in global namespace. We need namespaces to avoid data and name collision when using with third party libraries or our code is part of large application.

NOTE: Data defined under a namespace is considered a single huge object. 

Example of JavaScript's namespaces

The following syntax first checks whether MyNameSpace is created already in this or another file. If not create the empty namespace, else use the existing namespace to do the function.

Class

Since Javascript is prototype based language, class do not exist. Instead functions are created and used as class. 

Example of JavaScript's classes

<!DOCTYPE html>
<html>

<head>
<script>
function number(x ,y){
    //variables accessed using “this” keyword
    this.x = x;
    this.y = y;
}
</script>    
</head>

<body>
<script>
    //Instantiate object from class
    var num = new number(4, 8);
</script>
</body>

    
</html>

Object

Object is instance of a class. They can be created using new keyword or using object initializer.

Example

var num = new Number(4, 8);//Object num is created

var num = {x: 4, y:8}; //Object num is created

Constructor

Constructor is a method which gets executed when object is created. In Javascript, we do not define an explicit constructor, as the function which gets executed when object is created, serves as constructor. This function sets the object properties and execute methods to prepare the object for use.

Example of JavaScript's constructors

<!DOCTYPE html>
<html>

<head>
<script>
    //Constructor and class: number
    function number(x ,y){
        this.x = x;    //Initializes when object is created
        this.y = y;    //Initializes when object is created
        console.log(“Variables initialized”);
}
</script>    
</head>

<body>
<script>
    var num = number(6, 9);
</script>
</body>

    
</html>

Property

Variables contained in every instance of object are properties. Properties are defined in class and initialized in constructor, when object is created. Accessing property within class is done using this keyword. Outside the class, the property is accessed using object name, objectName.property.

Example of JavaScript's properties

Method

Method is a function attached with object. It defines action on the property. The various ways of defining methods in a class as given below.

Example #1 of JavaScript's methods

<!DOCTYPE html>
<html>    

<body>
<script>
    function number(x ,y){
        //variables accessed using “this” keyword
        this.x = x;
        this.y = y;
        //Calling method
        this.sum = getSum;
    }
    function getSum(){
        return this.x + this.y;
    }
    //Instantiate object from class
    var num = new number(4, 8);
    alert(num.getSum());
</script>
</body>

</html>

Methods can be defined as separate function. But all these are stored in global space. Defining many such functions lead to naming conflicts.

Example #2 of JavaScript's methods

<!DOCTYPE html>
<html>    

<body>
<script>
    function number(x ,y){
        this.x = x;
        this.y = y;
        //Function within constructor
        this.sum = function(x, y){
            return (this.x + this.y);
        };
    }
    var num = new number(4, 8);
    alert(num.getSum());
</script>
</body>   

</html>

Defining method within constructor leads to creating the sum() function every time an object is created.

Example #3 of JavaScript's methods

<!DOCTYPE html>
<html>    
        
<body>
<script>
    function number(x ,y){
        this.x = x;
        this.y = y;
    }
    number.prototype.getSum = function(){
        return this.x + this.y;
    };
    var num = new number(4, 8);
    alert(num.getSum());
</script>
</body>

</html>

Defining the method through prototype is better way of defining objects.

Inheritance

Inheritance means an object inherits methods and properties from parent object. It is effective reusing of code, where objects can have its parents and also its own, methods and properties. Javascript is prototype language where object is inherited from another object as opposed to classical languages like C+ or Java where class is inherited from another class.

Example of JavaScript's inheritance

Animal (Parent object)
{methods and properties: haveIntelligence,  eat();}

Wild(Child Object)                        
{name,  live(); }    -> Own methods.
(haveIntelligence,  eat();) -> Inherited methods.

In the short exampl above, the Animal is a general class which defines general behavior of the animal – eat, sleep, live. Lion and cow are sub class which can inherit the methods from Animal class.  

Steps to inherit class:

  • ChildClassName.prototype = new ParentClass();
  • Reset constructor property for class using ChildClassName.prototype.constructor = ChildClassName
  • We call the parent class methods overridden in child class using function call() method

Example of JavaScript's complex inheritance

Polymorphism

One function name defined in many ways is called polymorphism. That is, a parent class function inherited in child class may have different implementation. This is also called method over-riding.

Example of JavaScript's polymorphism

The eat() function in above example is implemented differently in Animal and WildAnimal classes. 

Encapsulation

Encapsulation is hiding of private properties and exposing methods to manipulate the data. JavaScript do not have public and private access specifiers as in other languages. The object variables with this keyword is public, the variables with var keyword is private.

Encapsulation ensures:

  • All the private data with var keyword and methods inside constructor are available for manipulation within object class only.
  • For outside the object, the properties can be accessed only by getter and setter functions.
  • Private methods cannot have access to public properties. They have to define a variable and assign the public property to the variable for its access.

Example of JavaScript's encapsulation

Abstraction

Abstraction in OOP's basically means declaring methods and hiding its implementation. This is of great use with encapsulation and inheritance features. Javascript does not support abstract and interface classes as in Java or C+. But abstraction can be achieved using prototype and is of great advantage in big applications.

Example of JavaScript's abstraction

 

›› go to examples ››