Adsense

A simple Javascript program

function emp(name,age){
this.name=name;
this.age=age;
this.getEmpInfo=getEmpInfo;
}

function getEmpInfo() {
   return 'Emp name : '+this.name + '\n Emp age: '+this.age;
}

var Employee = new emp("muthunce",30);
console.log(Employee.getEmpInfo());


Output:
 Emp name : muthunce
 Emp age: 30


Using object literals

var emp = {
name : "muthunce",
age : 30,
getEmpInfo : function() {
return "Emp name : "+this.name+ " ; Emp age : "+this.age;
}
}

emp.name="muthu";
console.log(emp.getEmpInfo());

Output:

 Emp name : muthu ; Emp age : 30


Singleton using a function

var emp = new function() {
   this.name="muthunce";
   this.age = 30;
   this.getInfo= function() {
      return "Emp Name : "+this.name+" Emp age : "+this.age;
   }
}

emp.name="muthu";
emp.getInfo();

Output:
"Emp Name : muthu Emp age : 30"




No comments:

Post a Comment

comment here

newest questions on wordpress