[removed] final / immutable global variables?

前端 未结 14 2061
既然无缘
既然无缘 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条回答
  • 2020-12-13 09:23

    This would be much cleaner approach

       var CONSTANTS = function() {
            var constants = { } ; //Initialize Global Space Here
            return {
                defineConstant: function(name,value)
                {
                    if(constants[name])
                    {
                       throw "Redeclaration of constant Not Allowed";
                    }
                },
                getValue(name)
                {
                   return constants[name];
                }
            } ;
        }() ;
        CONSTANTS.defineConstant('FOO','bar') ;
        console.log(CONSTANTS.getValue('FOO')) ; //Returns bar
        CONSTANTS.defineConstant('FOO','xyz') ; // throws exception as constant already defined
        CONSTANTS.getValue('XYZ') ; //returns undefined
    
    0 讨论(0)
  • 2020-12-13 09:24

    the const keyword?

    0 讨论(0)
  • 2020-12-13 09:27
    Object.defineProperty(window, 'CONSTANT_NAME', {value: CONSTANT_VALUE});
    
    // usage
    console.log(CONSTANT_NAME);
    

    Object.defineProperty() creates a property with the following default attributes:

    • configurable true if and only if the type of this property descriptor may be changed and if the property may be deleted from the corresponding object. Defaults to false.

    • enumerable true if and only if this property shows up during enumeration of the properties on the corresponding object. Defaults to false.

    • writable true if and only if the value associated with the property may be changed with an assignment operator. Defaults to false.

    if the "constant" is an object you might additionally want to make it immutable by freezing it. obj =Object.freeze(obj). have in mind that child-property-objects are not automatically frozen.

    0 讨论(0)
  • 2020-12-13 09:29

    yes the const is short for constant or final in some languages. google "javascript variable const" or constant to double i have even tested it myself so

    const yourVar = 'your value';
    

    thats what you are looking for.

    0 讨论(0)
  • 2020-12-13 09:30

    You might want to try out this jquery plugin. It prevents you to create global objects in javascript :)

    example code

    Store data

    // 'val' can be a string, integer, hash table, array, object
    $.secret( 'in', 'secretName', val );
    
    // or a function
    $.secret( 'in', 'secretName', function( arg1, arg2, arg3 ){
      // do something here
    });
    

    Use data; you can even use it in different files.

    var lang = $.secret( 'out', 'lang' );
    

    Call out a function

    $.secret( 'call', 'secretName', [ arg1, arg2, arg3 ]);
    // or
    $.secret( 'call', 'secretName', arg );
    

    Clear data

    $.secret( 'clear', 'lang' );
    

    source code is on github

    0 讨论(0)
  • 2020-12-13 09:34

    I know this question is old, but you could use Object.freeze(yourGlobalObjectHere); I just wrote a blog post about it here.

    0 讨论(0)
提交回复
热议问题