3

We have created a JSON constant we are sharing with various LWC. While we can define the JSON reference as const, the properties on the object can be changed by calling code.

For example:

const constants = {
    label: '30 years',
    value: 30
};

While we cannot do this:

constants = 'ABC';

We can do this:

constants.label = '20 years';

Is there a way to lock down the properties of the JSON so that they cannot be changed?

1 Answer 1

3

At a basic level, you can just freeze the object:

const constants = { label: '30 years', value: 30 }
Object.freeze(constants);

You can also use an immutable membrane, which you can start by using salesforce's ObservableMembrane implementation. Unlike Object.freeze, this protects an object's properties recursively, making the entire object graph immutable.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.