La propiedad del prototipo le permite agregar propiedades y métodos a un objeto.
Ejemplo
function employee(id:number,name:string) {
this.id = id
this.name = name
}
var emp = new employee(123,"Smith")
employee.prototype.email="[email protected]"
console.log("Employee 's Id: "+emp.id)
console.log("Employee's name: "+emp.name)
console.log("Employee's Email ID: "+emp.email)
Al compilar, generará el siguiente código JavaScript:
//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 = "[email protected]";
console.log("Employee 's Id: " + emp.id);
console.log("Employee's name: " + emp.name);
console.log("Employee's Email ID: " + emp.email);
Su salida es la siguiente:
Employee’s Id: 123
Emaployee’s name: Smith
Employee’s Email ID: [email protected]