Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properties of objects constantified by node-constants can be modified. #4

Open
ghost opened this issue Sep 28, 2016 · 1 comment
Open

Comments

@ghost
Copy link

ghost commented Sep 28, 2016

Since node-constants is using Object.defineProperty, properties of objects constantified by node-constants can be modified. Is this expected behavior? If isn't, you can use Object.freeze to make objects unmodifiable.

"use strict";

let define = require("node-constants")(global);
define("objectConstant", {value: 1});

console.log("Initial: ", objectConstant.value);
// 1

try {objectConstant = {value: 100}; /*Cannot do this, since `objectConstant` is not writable.*/}
catch(cannot) {console.error(cannot);}
console.log("After `objectConstant = {value: 100};`: ", objectConstant.value);
// 1

objectConstant.value = 2; // But this is able.
console.log("After `objectConstant.value = 2;`: ", objectConstant.value);
// 2

Object.freeze(objectConstant);
try {objectConstant.value = 10;}
catch(cannot) {console.error(cannot);}
console.log("After freezing the object & `objectConstant.value = 10;`: ", objectConstant.value);
// 2
@gowthamgts
Copy link

Yeah, this happens to me as well. I don't think, the properties are write-protected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant