2013-06-11 73 views
0

我有一個名爲FileController的'class',其中存儲了一個靜態屬性。對於它的價值,我用它來識別一個事件類型。當我嘗試訪問字符串作爲'類'的靜態屬性時,它的未定義。我想知道爲什麼?爲什麼這個靜態屬性未定義?如何訪問Javascript中的靜態屬性

FileController = function(galId) 
{ 

FileController.GALLERY_UPLOAD_START = "galleryUploadStart"; 
} 

//然後在另一個文件...

function initDragSystem() 
{ 
console.log('@initDragSystem FileController ' + FileController); //Traces out the constructor method 
console.log('@initDragSystem FileController.GALLERY_UPLOAD_START = ' +  FileController.GALLERY_UPLOAD_START) //traces out 'undefined' 

} 
+0

將屬性初始化行移到函數外,所以不必爲了定義而調用它 – Ian

回答

3

你需要調用或第一次調用函數,它被調用後,則該屬性設置: -

FileController(123); 

console.log(FileController.GALLERY_UPLOAD_START);//Now this will work. 

FileController = function(galId) 
{ 


} 

FileController.GALLERY_UPLOAD_START = "galleryUploadStart"; 
+2

或者,將設置「static」變量的行移動到構造函數的外部。 –

+1

是的,如果你想在不調用它的情況下訪問,請將靜態變量移到該函數的外部。 – pvnarula

+0

謝謝,反正有沒有像調用函數那樣訪問屬性就像類的靜態屬性,類似於Math.pi之類的東西? –