[removed] final / immutable global variables?

前端 未结 14 2087
既然无缘
既然无缘 2020-12-13 09:03

I think I know the answer but... is there any way to prevent a global variable from being modified by later-executing

14条回答
  •  猫巷女王i
    2020-12-13 09:18

    Conventions and good Documentation.

    You can prefix your "immutable" variable with two (or more) underscores to indicate that is something not meant to be used by others and to avoid other people's variables clashing with yours.

    Maybe creating a 'namespace' like __GLOBALNAMESPACE (Ugly name, I know) and then adding your variables into it (eg __GLOBALNAMESPACE.my_var) and creating a method like this one to retrieve them:

    getVariable(string name){
      return __GLOBALNAMESPACE[name]
    }
    

    Just my 2 cents.

提交回复
热议问题