Static variables in JavaScript

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

How can I create static variables in Javascript?

30条回答
  •  野的像风
    2020-11-22 02:35

    If you wanted to make a global static variable:

    var my_id = 123;
    

    Replace the variable with the below:

    Object.defineProperty(window, 'my_id', {
        get: function() {
                return 123;
            },
        configurable : false,
        enumerable : false
    });
    

提交回复
热议问题