为什么这个静态属性没有定义?如何在Javascript中访问静态属性

Why is this static property coming up undefined? How to access static properties in Javascript

本文关键字:属性 静态 Javascript 访问 定义 为什么      更新时间:2023-09-26

我有一个名为FileController的'类',我在其中存储了一个静态属性。不管它的价值是什么,它是我用来识别事件类型的字符串。当我试图访问字符串作为"类"的静态属性时,它的未定义。我想知道为什么?

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'
}

您需要首先调用或调用函数,在调用后,然后设置属性:-

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

FileController = function(galId)
{

}
FileController.GALLERY_UPLOAD_START = "galleryUploadStart";