Skip to content

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

Open
@ghost

Description

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions