Can global constants be declared in JavaScript?

前端 未结 8 815
耶瑟儿~
耶瑟儿~ 2020-12-16 09:33

If so, what is the syntax for such a declaration?

相关标签:
8条回答
  • 2020-12-16 09:38

    You could do it with getters and setters like so:

    Object.defineProperty(window, 'TAU', { 
        get: function(){return Math.PI*2;}
    });
    

    If you want a general function to do this:

    function define(name, value){
        Object.defineProperty(window, name, { 
           get: function(){return value;},
           set: function(){throw(name+' is a constant and cannot be redeclared.');},
        });
    }
    
    // Example use
    define('TAU', Math.PI*2);
    
    0 讨论(0)
  • 2020-12-16 09:39

    Javascript doesn't really have the notion of a named constant, or an immutable property of an object. (Note that I'm not talking about ES5 here.)

    You can declare globals with a simple var declaration in the global scope, like outside any function in a script included by a web page:

    <script>
      var EXACTLY_ONE = 1;
    

    Then your code can use that constant of course, though it's not really "constant" because the value can be changed (the property updated, in other words).

    edit — this is an ancient answer to an ancient question. In 2019, there's the const declaration that's supported just about everywhere. However note that like let, const scoping is different from var scoping.

    0 讨论(0)
  • 2020-12-16 09:46

    For the record.

    // ES6+ code:
    const CONSTGLOBAL1=200;  // it is a constant global
    
    function somef() { 
       document.write(CONSTGLOBAL1); // CONSTGLOBAL1 is defined (because it's global)
       const CONSTLOCAL=200; // it's a local constant
       document.write(CONSTLOCAL); // CONSTLOCAL is defined
    }       
    somef();
    document.write(CONSTLOCAL); // CONSTLOCALis NOT defined.
    

    So, if the constant is defined inside {} then it's local, otherwise, it's global.

    0 讨论(0)
  • 2020-12-16 09:49

    The direct answer to the question is No. It would really help though if ECMA/JS made a way to easily do functional programming. A workable hack I use to get around this is to declare a const in the global scope and use a wrapper function see example below:

    :)

    global_var = 3; //This can change say inside a function etc. but once you decide to make 
                    //this global variable into a constant by calling on a function
    const make_variable_constant = function(variable)
    {
        const constant = variable;
        return constant;
    }
    
    const const_global = make_variable_constant(global_var);
    

    :)

    Way back when object oriented programming was the hype a kid in my class told the C instructor that C isn't object oriented to which the instructor said he's done object oriented programming in C before Java and C++ were even conceived. Likewise you can do functional programming in Javascript but its much harder. Its like doing Object-oriented programming in C when its easier to do it in C++.

    0 讨论(0)
  • 2020-12-16 09:51

    If you want to make sure the value cannot change use a function.

    So, instead of:

    var Const_X=12
    

    use:

    function Const_X() {
    return 12;
    }
    
    0 讨论(0)
  • 2020-12-16 09:52

    As "Pointy" so carefully notes, ECMAscript has no such feature. However, JavaScript does:

    const a = 7;
    document.writeln("a is " + a + ".");
    

    Of course, if you're writing code to put on the web to run in web browsers, this might not help you much. :-)

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