Skip to content

Instantly share code, notes, and snippets.

@hkkcngz
Last active July 16, 2024 18:33
Show Gist options
  • Save hkkcngz/b1ade54e6fbecb8556135508e5c138db to your computer and use it in GitHub Desktop.
Save hkkcngz/b1ade54e6fbecb8556135508e5c138db to your computer and use it in GitHub Desktop.
Javascript Blocking Adding New Key Value to Object

Prevent Object from adding new Properties in JavaScript

Learn how to use preventExtensions to stop adding new properties to a JavaScript Object

The Object.preventExtensions() method prevents new properties from added to an object.

`` "use strict"; let user = { name : "Jagathish" };

Object.preventExtensions(user);

try { user.age = 23; } catch (e) { console.log(e); // expected output: TypeError:Cannot add property age, object is not extensible } ``

In the above code, after calling preventExtensions on the user object, when we try to add age property JavaScript will thow TypeError .

preventExtensions method returns the object being made non-extensible. There is no way to make an object extensible again once it has been made non-extensible. To check if the object is non-extensible we can use Object.isExtensible()

`` "use strict"; let user = { name : "Jagathish" };

Object.preventExtensions(user);

try { user.age = 23; } catch (e) { console.log(e); // expected output: TypeError:Cannot add property age, object is not extensible } ``

preventExtensions()method only prevents addition of own properties. Properties can still be added to the object prototype.

`` "use strict"; let user = { name : "Jagathish" };

Object.preventExtensions(user);

// user.age = 23; --> this will throw error

user.proto.age = 23; // adding property to object prototype

console.log(user); // {name : "Jagathish"}

console.log(user.age); // 23 --> this value comes from object prototype property ``

preventExtensions() makes the [[prototype]] of the user object immutable, any [[prototype]] re-assignment(adding new property) will throw a TypeError. This behavior is specific to the internal [[prototype]] property, other properties of the target object will remain mutable.

In other word when we try to change the proto prototype of the non-extensible object , JavaScript will throw error

"use strict"; let empty = Object.preventExtensions({}); empty.__proto__ = { name: 'jagathish' }; // TypeError : #<Object> is not extensible

You can delete the property of the non-extensible object

`` "use strict"; let user = { name : "Jagathish", age : 23 };

Object.preventExtensions(user);

delete user.age;

console.log(user); //{name: "Jagathish"} ``

You can also prevented from adding properties by using defineProperty

`` "use strict"; let user = { name : "Jagathish" };

Object.preventExtensions(user);

try { Object.defineProperty(user, 'age', { value: 23 }); } catch (e) { console.log(e); // expected output: TypeError:Cannot define property age, object is not extensible } ``

A non-object argument to preventExtensions will be treated as if it was a non-extensible ordinary object, return it. Object.preventExtensions(1); // returns 1

Source: jagathishsaravanan https://jagathishsaravanan.medium.com/prevent-object-from-adding-new-properties-in-javascript-67de5bf229c0#:~:text=The%20Object.,object%20being%20made%20non%2Dextensible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment