guard against accidental modifications

an object without prototype.

JSvalueobjectcreateObject.create() ⟩ pure object

Object.create() can be used to protect objects from being modified accidentally.

// the object to protect
const _protected = {
    data: 'DO NOT CHANGE THIS!',
};

// ⭐ guard against accidental modifications
const inherited = Object.create(_protected);

// ⭐ this modifies `inherited` itself, not the `protected` one.
inherited.data = 'Hello, World.';    

inherited.data,     // 'Hello, World.'
_protected.data,    // 'DO NOT CHANGE THIS!'

Last updated