2013-06-11 69 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之类的东西? –