Javescript Practice Day2 : Static members (variable and functions)
Static variables - to declare a static variable use constructor function name in the declaration of the members and same to invoke as show in the below example
function simpleInterst(tenure)
{
//public instance variable
this.t = tenure;
//principle and rate will be same so we are creating a static field
//static members are declared with the constructor function name
simpleInterst.principle=10000;
simpleInterst.rate=3;
this.rateOfInterest= function()
{
//static members are invoked with the constructor function name
return (simpleInterst.principle * simpleInterst.rate * this.t)/100;
}
}
var year = new simpleInterst(5);
document.write("<p> 5 years </p>"+year.rateOfInterest() + "<br/>");
var year = new simpleInterst(6);
document.write("<p> 6 years </p>"+year.rateOfInterest() + "<br/>");
var year = new simpleInterst(10);
document.write("<p> 10 years </p>"+year.rateOfInterest() + "<br/>");
Static Functions/methods - static methods are accessed with the help of constructor function name
function countObject(name)
{
this.name=name;
countObject.count=++countObject.count|| 1;
countObject.getCountObject=function()
{
return countObject.count;
}
}
var obj1= new countObject("obj1");
var obj2= new countObject("obj2");
var obj3= new countObject("obj3");
var obj4= new countObject("obj4");
//static methods are accessed with the help of constructor function name
document.write(countObject.getCountObject()+"<h1> Asscessing a static function with fucntion name</h1>");
function simpleInterst(tenure)
{
//public instance variable
this.t = tenure;
//principle and rate will be same so we are creating a static field
//static members are declared with the constructor function name
simpleInterst.principle=10000;
simpleInterst.rate=3;
this.rateOfInterest= function()
{
//static members are invoked with the constructor function name
return (simpleInterst.principle * simpleInterst.rate * this.t)/100;
}
}
var year = new simpleInterst(5);
document.write("<p> 5 years </p>"+year.rateOfInterest() + "<br/>");
var year = new simpleInterst(6);
document.write("<p> 6 years </p>"+year.rateOfInterest() + "<br/>");
var year = new simpleInterst(10);
document.write("<p> 10 years </p>"+year.rateOfInterest() + "<br/>");
Static Functions/methods - static methods are accessed with the help of constructor function name
function countObject(name)
{
this.name=name;
countObject.count=++countObject.count|| 1;
countObject.getCountObject=function()
{
return countObject.count;
}
}
var obj1= new countObject("obj1");
var obj2= new countObject("obj2");
var obj3= new countObject("obj3");
var obj4= new countObject("obj4");
//static methods are accessed with the help of constructor function name
document.write(countObject.getCountObject()+"<h1> Asscessing a static function with fucntion name</h1>");
Comments
Post a Comment