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
3 Answers
Reset to default 2You 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
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744370349a4570916.html
评论列表(0条)