2012-11-28 32 views
6
export class Entity { 
    add(component: Component, componentClass?: { new(): Component;}): Entity { 
     if (!componentClass) { 
      componentClass = component.constructor 
     } 

     /** sniiiiip **/ 
    } 
} 

的例子(分配component.constructor)的4行導致編譯器抱怨:訪問.constructor

屬性「構造」不上類型的值存在'組件'

什麼是正確的方式來獲取對象構造函數的引用?我的理解是,JavaScript中的所有對象都具有指向構造函數的.constructor屬性,該屬性指向用於創建該對象的構造函數...

回答

5

這在代碼爲Object的定義中未包含的類型代碼中非常罕見。你可以簡單地轉換爲any代替:

componentClass = (<any>component).constructor; 
相關問題