Demo entry 6783733

How to use Object.freeze

   

Submitted by anonymous on Feb 19, 2019 at 16:27
Language: JavaScript. Code size: 452 Bytes.

'use strict'

//Notice the 'use strict' above, if this is not used you will not see an error.

//Declare our immutable object using the "const" keyword.
//
const myImmutableObject = {
    property1: 42
  };
  
  const myReallyImmutableObject = Object.freeze(myImmutableObject);
  
  myReallyImmutableObject.property1 = 33;
  // Throws an error in strict mode
  
  console.log(myReallyImmutableObject.property1);
  // expected output: 42

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).