Just a small function to freeze the entire object avoiding mutability.
npm install deep-freezer
import deepFreeze from 'deep-freezer';
let person = {
firstName: 'name',
lastName: 'lastName'
};
deepFreeze(person);
To throw error when you try to mutate a frozen object, just use the deep-freezer in strict mode.
'use strict'
import deepFreeze from 'deep-freezer';
let person = {
firstName: 'name',
lastName: 'lastName'
};
deepFreeze(person);
More examples could be find at example folder.
///Throwing Errors.
'use strict'
import deepFreeze from 'deep-freezer';
let person = {
firstName: 'name',
lastName: 'lastName'
};
person.firstName = 'New Name'; //==> this line will throw an error.
deepFreeze(person);
OR
///Ignoring the errors, but still keeping the object frozen.
import deepFreeze from 'deep-freezer';
let person = {
firstName: 'name',
lastName: 'lastName'
};
person.firstName = 'New Name'; //==> this line will be ignored.
deepFreeze(person);
npm t