最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - NodeJs define global constants - Stack Overflow

programmeradmin8浏览0评论

im wondering how to define global constants in node js.

My approach so far:

constants.js:

module.exports = Object.freeze({
MY_CONST: 'const one'});

controller.js:

var const = require(./mon/constants/constants.js);
console.log(const.MY_CONST) ==> const one
const.MY_CONST ='something'
console.log(const.MY_CONST) ==> const one

Ok thats fine so far. But then i wanted to structure my constants like this:

constants.js:

module.exports = Object.freeze({
    MY_TOPIC: {
        MY_CONST: 'const one'
    }
});

controller.js:

var const = require(./mon/constants/constants.js);
console.log(const.MY_TOPIC.MY_CONST) ==> const one
const.MY_TOPIC.MY_CONST ='something'
console.log(const.MY_TOPIC.MY_CONST) ==> something

Hmm no MY_CONST is not constant any more... How can i solve this problem?

im wondering how to define global constants in node js.

My approach so far:

constants.js:

module.exports = Object.freeze({
MY_CONST: 'const one'});

controller.js:

var const = require(./mon/constants/constants.js);
console.log(const.MY_CONST) ==> const one
const.MY_CONST ='something'
console.log(const.MY_CONST) ==> const one

Ok thats fine so far. But then i wanted to structure my constants like this:

constants.js:

module.exports = Object.freeze({
    MY_TOPIC: {
        MY_CONST: 'const one'
    }
});

controller.js:

var const = require(./mon/constants/constants.js);
console.log(const.MY_TOPIC.MY_CONST) ==> const one
const.MY_TOPIC.MY_CONST ='something'
console.log(const.MY_TOPIC.MY_CONST) ==> something

Hmm no MY_CONST is not constant any more... How can i solve this problem?

Share Improve this question asked Aug 29, 2016 at 12:16 johni07johni07 7911 gold badge11 silver badges31 bronze badges 1
  • 1 const is not a valid variable name. – Bergi Commented Aug 29, 2016 at 12:20
Add a ment  | 

3 Answers 3

Reset to default 2

You need to freeze inner object too. Something like that

module.exports = Object.freeze({
    MY_TOPIC: Object.freeze({
        MY_CONST: 'const one'
    })
});

Demo

var consts = Object.freeze({
  MY_TOPIC: Object.freeze({
    MY_CONST: 'const one'
  })
});

console.log(consts.MY_TOPIC.MY_CONST);
consts.MY_TOPIC.MY_CONST = "something";
console.log(consts.MY_TOPIC.MY_CONST);

You can nest your freeze calls, but I think what you actually want is

// constants.js
module.exports = Object.freeze({
    MY_CONST: 'const one'
});

// controller.js
const MY_TOPIC = require(./mon/constants/constants.js);
console.log(MY_TOPIC.MY_CONST) // ==> const one
MY_TOPIC.MY_CONST = 'something'; // Error
console.log(MY_TOPIC.MY_CONST) // ==> const one

Object values of the frozen object can be changed. Read the following example from the Object.freeze() doc to freeze all your object:

obj1 = {
  internal: {}
};

Object.freeze(obj1);
obj1.internal.a = 'aValue';

obj1.internal.a // 'aValue'

// To make obj fully immutable, freeze each object in obj.
// To do so, we use this function.
function deepFreeze(obj) {

  // Retrieve the property names defined on obj
  var propNames = Object.getOwnPropertyNames(obj);

  // Freeze properties before freezing self
  propNames.forEach(function(name) {
    var prop = obj[name];

    // Freeze prop if it is an object
    if (typeof prop == 'object' && prop !== null)
      deepFreeze(prop);
  });

  // Freeze self (no-op if already frozen)
  return Object.freeze(obj);
}

obj2 = {
  internal: {}
};

deepFreeze(obj2);
obj2.internal.a = 'anotherValue';
obj2.internal.a; // undefined
发布评论

评论列表(0)

  1. 暂无评论