I'm trying to implement an object in which there can be an arbitrary number of properties and a function method that receives the input parameter string and checks if there is a property with that name. Properties and method cannot be overridden, deleted, and new functionality cannot be added to the object. I need this to resemble enuma.

var SomeObj = {}; Object.defineProperties(SomeObj, { prop1: { value: 1, writable: false, configurable: false, enumerable: true }, isAllowed: { value: function (type) { return this.hasOwnProperty(type); }, writable: false, configurable: false, enumerable: true } }); Object.preventExtensions(SomeObj); 

It’s embarrassing that in the articles I didn’t see such a syntax ( value: function () {} ), besides autocompletion of phpstorm on the SomeObj method shows as a property (without brackets at the end that need to be manually delivered) which is not surprising because it as a property and declared, but it is not obvious. In a word, is there another way to declare a method for an object that cannot be later redefined.

2 answers 2

Maybe you are looking for Object.freeze or Object.seal ?

From MDN: The Object.freeze () method freezes an object: this means that it prevents the addition of new properties to an object, the removal of old properties from an object, and the modification of existing properties or the value of their attributes of enumeration, customizability, and recordability.

    Only inside the object to determine the private properties and methods through var.