Static variables in JavaScript

后端 未结 30 2256
别那么骄傲
别那么骄傲 2020-11-22 01:55

How can I create static variables in Javascript?

30条回答
  •  醉酒成梦
    2020-11-22 02:33

    If you want to declare static variables for creating constants in your application then I found following as most simplistic approach

    ColorConstants = (function()
    {
        var obj = {};
        obj.RED = 'red';
        obj.GREEN = 'green';
        obj.BLUE = 'blue';
        obj.ALL = [obj.RED, obj.GREEN, obj.BLUE];
        return obj;
    })();
    
    //Example usage.
    var redColor = ColorConstants.RED;
    

提交回复
热议问题