Static Members & Prototype In JavaScript 2020

In this article we will Understand Static Members In JavaScript ,How to declare Static Variables and Prototype In JavaScript by Sagar Jaybhay.

Static Members In JavaScript

Static properties and methods are those that don’t change with one instance to another instance.

Also, if we declared static function then these functions can be used without creating an object of a class.

In javascript, there is no special syntax to denote static member but we can make use of constructor function name to declare static fields or static methods in Javascript.

The static variable is not created every time when an object is created.

function Shape(){

    //here count is variable and it is static declaration contains constructor function name
    Shape.Count=++Shape.Count||1;

    // this is static function 
    Shape.GetTotalCount=function(){
        return Shape.Count;
    }

    // below is the instance variable
    this.val=0;
}

var shape1=new Shape();
var shape2=new Shape();
var shape3=new Shape();

alert(Shape.GetTotalCount());

Prototype In Javascript

All javascript object inherits properties and method from a Prototype.

Javascript is a prototype-based language when we create a function using JavaScript, the engine adds a prototype property inside a function and the prototype basically is an object also It is called a prototype object.

A prototype is a special kind of enumerable object to which additional properties can attach to it and it will share across all the instances of that constructor function.

function Employee(name)
{

    this.FullName=name;
}

var emp=new Employee("SAGAR JAYBHAY");
emp.greeting=function(){
    return "Hello, "+this.FullName;
}



var emp2=new Employee("XYZ");
emp2.

In the above code emp2 is object and greeting function we created on emp object. In javascript is dynamic language so we are able to create function but it is not accessible to the rest of the object. To overcome this kind of situation we can use the prototype.

function Employee(name)
{

    this.FullName=name;
}

var emp=new Employee("SAGAR JAYBHAY");
// emp.greeting=function(){
//     return "Hello, "+this.FullName;
// }

Employee.prototype.greeting=function(){
    return "Hello, "+this.FullName;
}

var emp2=new Employee("XYZ");
emp2.greeting();

in the above code, we have created a greeting function by using a prototype and by that way it is accessible to all objects.

Prototype function allows you to override function it’s required.


GitHub Files:- https://github.com/Sagar-Jaybhay/JavaScriptInBrowser


Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer at Software Company. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.

Sagar Jaybhay

Sagar Jaybhay, from Maharashtra, India, is currently a Senior Software Developer at Software Company. He has continuously grown in the roles that he has held in the more than seven years he has been with this company. Sagar Jaybhay is an excellent team member and prides himself on his work contributions to his team and company as a whole.

You may also like...