TypeScript – Prototype property

 prototype property

The prototype property allows you to add properties and methods to an object.

Example

function employee(id:number,name:string) { 
   this.id = id 
   this.name = name 
} 
var emp = new employee(123,"Smith") 
employee.prototype.email="smith@abc.com" 
console.log("Employee 's Id: "+emp.id) 
console.log("Employee's name: "+emp.name) 
console.log("Employee's Email ID: "+emp.email)

On compiling, it will generate the following JavaScript code −

//Generated by typescript 1.8.10 
function employee(id, name) { 
   this.id = id; 
   this.name = name; 
} 
var emp = new employee(123, "Smith"); 
employee.prototype.email = "smith@abc.com"; 
console.log("Employee 's Id: " + emp.id); 
console.log("Employee's name: " + emp.name); 
console.log("Employee's Email ID: " + emp.email);

Its output is as follows −

Employee’s Id: 123 
Emaployee’s name: Smith 
Employee’s Email ID: smith@abc.com

Previous Page:-Click Here

Leave a Reply