Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Hidayt Rahman
Hidayt Rahman

Posted on

     

Make Object readOnly by using Object.freeze()

TheObject.freeze() method freezes an object. A frozen object can no longer be changed; freezing an object prevents new properties from being added to it, existing properties from being removed, prevents changing the enumerability, configurability, or writability of existing properties, and prevents the values of existing properties from being changed. In addition, freezing an object also prevents its prototype from being changed. freeze() returns the same object that was passed in.

Create an abject

constperson={name:"hidayt",location:"delhi"}
Enter fullscreen modeExit fullscreen mode

AddObject.freeze() method just after it.

// restrictionObject.freeze(person);
Enter fullscreen modeExit fullscreen mode

Try to update location property of the object and log it

// update locationperson.location="Mumbai";console.log(person.location);// delhi
Enter fullscreen modeExit fullscreen mode

Yes, It will not allow to update the object.

Note: Always putObject.freeze() after the object, Which you want to restrict and pass that object into thefreeze() method.

Ref:Object.freeze()

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I am passionate programmer and love travelling and photography
  • Location
    Delhi, India
  • Education
    MCA
  • Work
    Frontend Architect
  • Joined

More fromHidayt Rahman

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp