Are there constants in JavaScript?

后端 未结 30 3159
抹茶落季
抹茶落季 2020-11-22 08:53

Is there a way to use constants in JavaScript?

If not, what\'s the common practice for specifying variables that are used as constants?

30条回答
  •  天命终不由人
    2020-11-22 09:30

    For a while, I specified "constants" (which still weren't actually constants) in object literals passed through to with() statements. I thought it was so clever. Here's an example:

    with ({
        MY_CONST : 'some really important value'
    }) {
        alert(MY_CONST);
    }
    

    In the past, I also have created a CONST namespace where I would put all of my constants. Again, with the overhead. Sheesh.

    Now, I just do var MY_CONST = 'whatever'; to KISS.

提交回复
热议问题