TypeScript Prototype屬性

2019-01-09 14:29 更新

TypeScript Prototype屬性

prototype屬性允許您向?qū)ο筇砑訉傩院头椒ā?/span>

示例

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)

在編譯時,它將生成以下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 = "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);

其輸出如下:

Employee’s Id: 123 
Emaployee’s name: Smith 
Employee’s Email ID: smith@abc.com
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號